// 默认刷新时间5秒等设置
var mainTimerCycle = 5;
var mainSetType;
var mainDevType;
var mainDevAddress;
var mainBfmUnitNo;
var mainBfmAddress;
var mainDataType;
var mainInputType;
var mainDevRegData;
var mainBitsortSel;
var mainTimerID_menu;
var mainTimerCount;
var mainTimeOutID;
var isBatMonitor;
var isRegMonitor;
var savedPage;
var tstDev;
var tstFmt;
var tstType;
var tstVal;
var tstUpdateFn;
var viewPageNum = 1;
var timerID_reg = 0;
var timerID_bat = 0;
var timerID_default = 0;
var timerID_cpudiag = 0;
var timerID_Acc = 0;
var mainMultiPoint;
var timerID_10 = 0;
var timeout_10 = 9999999;
var mainBlSelect;
var mainBlAddress;

//XHR请求以及HTML颜色跟登出
var HDTYP = "Content-Type",
  HDVAL = "application/x-www-form-urlencoded",
  LINE_HOVER_COLOR = "rgb(207,221,242)",
  LINE_HOVER_COLOR2 = "rgb(189,205,226)",
  LINE_HOVER_STRCOLOR = "rgb(13,81,179)",
  logoutMsg = 0;

//设置HTML元素element内的值
function setElIdVal(a, b, c) {
  1 == c
    ? (viewarea.document.getElementById(a).value = b)
    : 2 == c
    ? (devTestFrame.document.getElementById(a).value = b)
    : (document.getElementById(a).value = b);
}
//设置HTML元素element内的文本
function setInTxt(a, b, c) {
  1 == c
    ? (viewarea.document.getElementById(a).innerText = b)
    : 2 == c
    ? (devTestFrame.document.getElementById(a).innerText = b)
    : (document.getElementById(a).innerText = b);
}
//设置HTML元素element的颜色
function setStlCol(a, b, c) {
  1 == c
    ? (viewarea.document.getElementById(a).style.color = b)
    : 2 == c
    ? (devTestFrame.document.getElementById(a).style.color = b)
    : (document.getElementById(a).style.color = b);
}
//设置HTML元素的背景色
function setBkCol(a, b, c) {
  1 == c
    ? (viewarea.document.getElementById(a).style.backgroundColor = b)
    : 2 == c
    ? (devTestFrame.document.getElementById(a).style.backgroundColor = b)
    : (document.getElementById(a).style.backgroundColor = b);
}
//获取HTML元素element内的值
function getElIdVal(a, b) {
  return 1 == b
    ? viewarea.document.getElementById(a).value
    : 2 == b
    ? devTestFrame.document.getElementById(a).value
    : document.getElementById(a).value;
}
//获取HTML元素element内的文本内容
function getInTxt(a, b) {
  return 1 == b
    ? viewarea.document.getElementById(a).innerText
    : 2 == b
    ? devTestFrame.document.getElementById(a).innerText
    : document.getElementById(a).innerText;
}
//获取HTML元素element对象
function getElIdObj(a, b) {
  return 1 == b
    ? viewarea.document.getElementById(a)
    : 2 == b
    ? devTestFrame.document.getElementById(a)
    : document.getElementById(a);
}
//禁用disabled，使不可输入
function setElDisable(a, b, c) {
  return 1 == c
    ? (viewarea.document.getElementById(a).disabled = b)
    : 2 == c
    ? (devTestFrame.document.getElementById(a).disabled = b)
    : (document.getElementById(a).disabled = b);
}
//错误消息弹出
function alertMessage(a, b) {
  var c = "";
  switch (b) {
    case 0:
      0 == logoutMsg && ((logoutMsg = 1), alert(getErrorStr(a)));
      returnLoginPage();
      break;
    case 1:
      c = getErrorStr(a);
      break;
    case 2:
      alert(getErrorStr(a));
      break;
    default:
      alert(a + ":" + getErrorStr(a));
  }
  return c;
}
//获取报错信息
function getErrorStr(a) {
  for (var b = "", c = 0; c < errorString.length; c++)
    if (a == errorString[c][0]) {
      b = errorString[c][1];
      break;
    }
  0 == b.length &&
    (b = "[ " + a + " ] : " + errorString[errorString.length - 1][1]);
  return b;
}
//获取显示字符
function getMonitorStr(a) {
  return monitorString[a];
}
//获取数据类型
function getDataTypeStr(a) {
  return dataTypeString[a];
}

function getRadixTypeStr(a) {
  for (var b = "--", c = 0; c < radixTypeString.length; c++)
    if (a == radixTypeString[c][0]) {
      b = radixTypeString[c][1];
      break;
    }
  return b;
}
//回到登陆页面
function returnLoginPage() {
  top.location.href = "http://" + location.host;
}

//日文页面返回0，英文1，中文2
function checkFileLanguage() {
  var a = 0,
    b = viewarea.location.href.split("/").pop();
  b.match("ja-JP")
    ? (a = 0)
    : b.match("en-US")
    ? (a = 1)
    : b.match("zh-CN") && (a = 2);
  return a;
}
//禁用登出页面
function logoutBtnDisable(a) {
  window.parent.document.getElementById("logoutbtn").disabled = a;
}

//获取设备变量数组
function getStorageArray(a) {
  if ("undefined" === typeof mainDevRegData) {
    mainDevRegData = [];
    for (var b = 0; b < 2 * MAX_DISP_LINE; b++) {
      mainDevRegData[b] = [];
      for (var c = 0; 4 > c; c++) mainDevRegData[b][c] = "";
    }
  }
  "undefined" === typeof sessionStorage
    ? console.log("unsupported sessionStorage")
    : sessionStorage.getItem(a) &&
      (mainDevRegData = JSON.parse(sessionStorage.getItem(a)));
}

//获取session存的内容
function getStorage(a, b) {
  var c = b;
  "undefined" === typeof sessionStorage
    ? console.log("unsupported sessionStorage")
    : sessionStorage.getItem(a) && (c = sessionStorage.getItem(a));
  return c;
}

//在session存内容
function setStorage(a, b) {
  try {
    "undefined" === typeof sessionStorage
      ? console.log("unsupported sessionStorage")
      : sessionStorage.setItem(a, b);
  } catch (c) {
    console.log(c.message);
  }
}

//会话保存当前页面
function saveCurrentPage(a, b) {
  viewarea.location.href.substring(
    viewarea.location.href.lastIndexOf("/") + 1,
    viewarea.location.href.length
  );
  setStorage("currentPage", b);
  onclickMenu(a);
}

//获取浏览器本地存储信息
function getlocalStorageArray(a) {
  if ("undefined" === typeof mainDevRegData) {
    mainDevRegData = [];
    for (var b = 0; b < 2 * MAX_DISP_LINE; b++) {
      mainDevRegData[b] = [];
      for (var c = 0; 4 > c; c++) mainDevRegData[b][c] = "";
    }
  }
  "undefined" === typeof localStorage
    ? console.log("unsupported localStorage")
    : localStorage.getItem(a) &&
      (mainDevRegData = JSON.parse(localStorage.getItem(a)));
}
//在浏览器本地存储信息
function setlocalStorage(a, b) {
  try {
    "undefined" === typeof localStorage
      ? console.log("unsupported localStorage")
      : localStorage.setItem(a, b);
  } catch (c) {
    console.log(c.message);
  }
}
function reloadChildFrame() {
  viewarea.location.reload(!0);
}
//浏览器本地存储信息，一般退出时用到
function localStorageClear() {
  localStorage.clear();
}
function arrayClear() {
  mainDevRegData = [];
  for (var a = 0; a < 2 * MAX_DISP_LINE; a++) {
    mainDevRegData[a] = [];
    for (var b = 0; 4 > b; b++) mainDevRegData[a][b] = "";
  }
}
function getMultiTypeStr(a) {
  for (var b = "--", c = 0; c < multiTypeString.length; c++)
    if (a == multiTypeString[c][0]) {
      b = multiTypeString[c][1];
      break;
    }
  return b;
}
function getAccStr(a) {
  for (var b = "", c = 0; c < actionString.length; c++)
    if (a == actionString[c][0]) {
      b = actionString[c][1];
      break;
    }
  return b;
}
function getPrtclStr(a) {
  for (var b = "", c = 0; c < protocolString.length; c++)
    if (a == protocolString[c][0]) {
      b = protocolString[c][1];
      break;
    }
  return b;
}


function modalOpenDevTest() {
  document.getElementById("modal-win").style.display = "block";
  document.getElementById("modal-devtest").style.display = "block";
  document.getElementById("devtest-frame").contentWindow.startDevTestWindow();
}

function modalCloseDevTest() {
  document.getElementById("modal-win").style.display = "none";
  document.getElementById("modal-devtest").style.display = "none";
}
//调整页面窗口显示大小
window.onresize = function () {
  var window_obj = window;
  html_obj = document.getElementsByTagName("HTML").item(0);
  html_obj.style.height = window.innerHeight + "px";
};

//JS补充
var $jscomp = { scope: {} };
$jscomp.defineProperty =
  "function" == typeof Object.defineProperties
    ? Object.defineProperty
    : function (b, c, a) {
        if (a.get || a.set)
          throw new TypeError("ES3 does not support getters and setters.");
        b != Array.prototype && b != Object.prototype && (b[c] = a.value);
      };
$jscomp.getGlobal = function (b) {
  return "undefined" != typeof window && window === b
    ? b
    : "undefined" != typeof global && null != global
    ? global
    : b;
};
$jscomp.global = $jscomp.getGlobal(this);
$jscomp.polyfill = function (b, c, a, d) {
  if (c) {
    a = $jscomp.global;
    b = b.split(".");
    for (d = 0; d < b.length - 1; d++) {
      var e = b[d];
      e in a || (a[e] = {});
      a = a[e];
    }
    b = b[b.length - 1];
    d = a[b];
    c = c(d);
    c != d &&
      null != c &&
      $jscomp.defineProperty(a, b, {
        configurable: !0,
        writable: !0,
        value: c,
      });
  }
};
$jscomp.polyfill(
  "Array.prototype.fill",
  function (b) {
    return b
      ? b
      : function (b, a, d) {
          var c = this.length || 0;
          0 > a && (a = Math.max(0, c + a));
          if (null == d || d > c) d = c;
          d = Number(d);
          0 > d && (d = Math.max(0, c + d));
          for (a = Number(a || 0); a < d; a++) this[a] = b;
          return this;
        };
  },
  "es6-impl",
  "es3"
);
//事件日志
var evClickRow = 0,
  evLog = [],
  reqPoint = 0,
  dateNum = 0,
  eveFlg = 0,
  refAddress = 0;
function onloadEventLog() {
  evClickRow = 0;
  saveCurrentPage("inmenu6", "EventLog");
  dateNum = getStorage("dateNum", "");
  var b = getStorage("evLog", null);
  evLog = null != b ? JSON.parse(b) : [];
  reqPoint = parseInt(getStorage("reqPoint", 0), 10);
  refAddress = getStorage("refAddress", "0");
  setInTxt("evdate", dateNum, 1);
  0 == reqPoint && disablePrevBtn(!0);
  null == b ? GetEventLogInf(reqPoint) : (renderEvLog(evLog), disableNextBtn());
  setElDisable("cycletext1", !0);
  setElDisable("cycletext2", !0);
  setElDisable("reloadcycle", !0);
  logoutBtnDisable(!1);
}
//事件日志
function eventlogCanvas(b, c) {
  var a = getElIdObj("eventlogCanvas" + (b + 1), 1),
    d = a.getContext("2d");
  d.clearRect(0, 0, a.width, a.height);
  0 > c ||
    4 < c ||
    (d.beginPath(),
    0 != c
      ? (d.moveTo(5, 20),
        d.lineTo(15, 5),
        d.lineTo(25, 20),
        d.closePath(),
        1 == c
          ? (d.fillStyle = "rgb(255,255,0)")
          : 2 == c
          ? (d.fillStyle = "rgb(255,127,0)")
          : 3 == c && (d.fillStyle = "rgb(255,0,0)"))
      : (d.arc(15, 15, 10, 0, 2 * Math.PI, !0),
        (d.fillStyle = "rgb(255,255,0)")),
    (d.lineWidth = 3),
    d.stroke(),
    d.fill());
}
//清除事件日志显示内容
function clearEventlogCanvas(b) {
  b = getElIdObj("eventlogCanvas" + b, 1);
  b.getContext("2d").clearRect(0, 0, b.width, b.height);
}
//获取事件日志信息
function GetEventLogInf(b) {
  eveFlg = 1;
  try {
    onTimerStopForW10();
    var c,
      a =
        "NUM=" +
        (32).toString(16) +
        "&TOPNO=" +
        b.toString(16) +
        "&ADDR=" +
        refAddress;
    c = new XMLHttpRequest();
    c.open("POST", "/cgi/GetEventLog.cgi", !0);
    c.setRequestHeader(HDTYP, HDVAL);
    c.onreadystatechange = function () {
      GetEventLogInf_Response(c);
    };
    c.send(a);
  } catch (d) {
    console.error(d.message);
  }
}

//获取事件日志信息对应内容
function GetEventLogInf_Response(b) {
  if (4 != b.readyState || 200 != b.status)
    3 == b.readyState && onTimerStartForW10();
  else {
    try {
      var c = !1,
        a = JSON.parse(b.response);
      switch (a.RET) {
        case "0000":
          "0000" == a.CMP
            ? (convertEvLog(a.EVENT),
              (refAddress = a.ADDR),
              setStorage("refAddress", refAddress))
            : "41FE" == a.CMP && alertMessage(a.CMP, 2);
          "41FE" != a.CMP &&
            0 == reqPoint &&
            (updateTime(a.TIME), setInTxt("evdate", dateNum, 1));
          break;
        case "0001":
          alertMessage(a.RET, 0);
          break;
        case "4021":
        case "4025":
        case "402C":
        case "41C5":
        case "4180":
        case "41FE":
        case "4C1F":
          alertMessage(a.RET, 2);
          break;
        default:
          console.error(a.RET);
      }
    } catch (d) {
      console.error(d.message);
    }
    if (
      ("0000" == a.RET && "0000" != a.CMP && 0 != reqPoint) ||
      ("0000" != a.RET && 0 != reqPoint && 0 != evLog.length)
    )
      (reqPoint -= 32), setStorage("reqPoint", reqPoint);
    renderEvLog(evLog);
    0 == reqPoint && (c = !0);
    disablePrevBtn(c);
    disableNextBtn();
    eveFlg = 0;
  }
}

//解读事件内容
function convertEvLog(b) {
  for (var c = [], a = 0; a < b.length; a++) {
    c[a] = [];
    var d, e;
    d = b[a][3].split("-");
    e = b[a][4].split(":");
    d =
      ("0000" + parseInt(d[0], 16)).slice(-4) +
      "-" +
      ("00" + parseInt(d[1], 16)).slice(-2) +
      "-" +
      ("00" + parseInt(d[2], 16)).slice(-2);
    e =
      ("00" + parseInt(e[0], 16)).slice(-2) +
      ":" +
      ("00" + parseInt(e[1], 16)).slice(-2) +
      ":" +
      ("00" + parseInt(e[2], 16)).slice(-2);
    c[a][1] = d + "\r\n" + e;
    c[a][0] = b[a][0];
    c[a][2] = b[a][2];
    c[a][3] = b[a][1];
    d = changeFileName(b[a][6]);
    c[a][4] = d.replace(/^\s+|\s+$/g, "");
    c[a][5] = b[a][7].replace(/^\s+|\s+$/g, "");
    c[a][6] = b[a][5].replace(/^\s+|\s+$/g, "");
    c[a][7] = b[a][8];
    c[a][8] = b[a][9];
  }
  Array.prototype.push.apply(evLog, c);
  setStorage("evLog", JSON.stringify(evLog));
}
//显示事件日志
function renderEvLog(b) {
  var c = reqPoint;
  evClickRow = 0;
  getElIdObj("eveinfo1", 1).innerText = "";
  getElIdObj("eveinfo2", 1).innerText = "";
  getElIdObj("eveinfo3", 1).innerText = "";
  for (var a = 0; 32 > a; a++)
    if (0 === b.length && 0 == a)
      (getElIdObj("number1", 1).innerText = "1"),
        (getElIdObj("daytime1", 1).innerText = getErrorStr("msg_noEv")),
        (getElIdObj("type1", 1).innerText = "-"),
        (getElIdObj("code1", 1).innerText = "-"),
        (getElIdObj("outline1", 1).innerText = "-"),
        (getElIdObj("state1", 1).innerText = ""),
        clearEventlogCanvas(1),
        (evLog = []),
        setStorage("evLog", JSON.stringify(evLog));
    else {
      if (void 0 != b[c]) {
        setInTxt("number" + (a + 1), reqPoint + a + 1, 1);
        var d = getTypeString(b[c][0]);
        setInTxt("type" + (a + 1), d, 1);
        setInTxt("daytime" + (a + 1), b[c][1], 1);
        setInTxt("state" + (a + 1), getStateStr(parseInt(b[c][2])), 1);
        eventlogCanvas(a, b[c][2]);
        setInTxt(
          "code" + (a + 1),
          parseInt(b[c][0], 16).toString() + b[c][3],
          1
        );
        d = getErrorString(b[c][3]);
        setInTxt("outline" + (a + 1), d, 1);
        "FF" == b[c][0] &&
          (setInTxt("state" + (a + 1), "", 1),
          clearEventlogCanvas(a + 1),
          setInTxt("code" + (a + 1), "*HST LOSS*", 1));
      } else
        setInTxt("number" + (a + 1), "", 1),
          setInTxt("type" + (a + 1), "", 1),
          setInTxt("daytime" + (a + 1), "", 1),
          setInTxt("state" + (a + 1), "", 1),
          clearEventlogCanvas(a + 1),
          setInTxt("code" + (a + 1), "", 1),
          setInTxt("outline" + (a + 1), "", 1);
      c++;
    }
}
//点击事件日志行触发
function onclickEventRow(b, c) {
  if ("" != b.innerText) {
    evClickRow = c;
    for (var a = 1; 32 >= a; a++) setEveLineColor(a, "", "");
    setEveLineColor(c, menuSelBgColor, "white");
    if (0 != evLog.length) {
      var a = getStepFileString(
          evLog[reqPoint + c - 1][4],
          evLog[reqPoint + c - 1][5]
        ),
        d = getUnitNoString(evLog[reqPoint + c - 1][6]),
        e = getPortIpaddrString(
          evLog[reqPoint + c - 1][7],
          evLog[reqPoint + c - 1][8]
        );
      "FF" == evLog[reqPoint + c - 1][0] && (e = d = a = "");
      setInTxt("eveinfo1", a, 1);
      setInTxt("eveinfo2", d, 1);
      setInTxt("eveinfo3", e, 1);
    }
  }
}

function onEveMouseOver(b) {
  var c;
  b != evClickRow &&
    ((c = 0 == b % 2 ? LINE_HOVER_COLOR2 : LINE_HOVER_COLOR),
    setEveLineColor(b, c, LINE_HOVER_STRCOLOR));
}

function onEveMouseOut(b) {
  b != evClickRow && setEveLineColor(b, "", "");
}

function setEveLineColor(b, c, a) {
  setBkCol("number" + b, c, 1);
  setBkCol("daytime" + b, c, 1);
  setBkCol("type" + b, c, 1);
  setBkCol("stateimage" + b, c, 1);
  setBkCol("state" + b, c, 1);
  setBkCol("code" + b, c, 1);
  setBkCol("outline" + b, c, 1);
  setStlCol("number" + b, a, 1);
  setStlCol("daytime" + b, a, 1);
  setStlCol("type" + b, a, 1);
  setStlCol("stateimage" + b, a, 1);
  setStlCol("state" + b, a, 1);
  setStlCol("code" + b, a, 1);
  setStlCol("outline" + b, a, 1);
}
function onunloadEventLog() {
  setElDisable("cycletext1", !1);
  setElDisable("cycletext2", !1);
  setElDisable("reloadcycle", !1);
  logoutBtnDisable(!0);
}

function onEventLoad() {
  1 != eveFlg &&
    ((evLog = []),
    (reqPoint = 0),
    setStorage("reqPoint", reqPoint),
    (dateNum = ""),
    setInTxt("evdate", dateNum, 1),
    (refAddress = "0"),
    GetEventLogInf(0));
}
function nextEvLog() {
  1 != eveFlg &&
    ((reqPoint += 32),
    setStorage("reqPoint", reqPoint),
    void 0 != evLog[reqPoint]
      ? (renderEvLog(evLog), disableNextBtn(), disablePrevBtn(!1))
      : GetEventLogInf(reqPoint + 1));
}

//小于32条禁用下一页按钮
function prevEvLog() {
  reqPoint -= 32;
  1 > reqPoint && ((reqPoint = 0), disablePrevBtn(!0));
  renderEvLog(evLog);
  disableNextBtn();
  setStorage("reqPoint", reqPoint);
}
//禁用前一页按钮
function disablePrevBtn(b) {
  1 == b
    ? setElDisable("prevevebutton", !0, 1)
    : setElDisable("prevevebutton", !1, 1);
}
//更新时间
function updateTime(b) {
  var c = b.split(" ");
  b = c[0].split("-");
  c = c[1].split(":");
  b =
    ("0000" + parseInt(b[0], 16)).slice(-4) +
    "-" +
    ("00" + parseInt(b[1], 16)).slice(-2) +
    "-" +
    ("00" + parseInt(b[2], 16)).slice(-2);
  c =
    ("00" + parseInt(c[0], 16)).slice(-2) +
    ":" +
    ("00" + parseInt(c[1], 16)).slice(-2) +
    ":" +
    ("00" + parseInt(c[2], 16)).slice(-2);
  dateNum = b + " " + c;
  setStorage("dateNum", dateNum);
}

//禁用下一页按钮
function disableNextBtn() {
  var b = getInTxt("number32", 1);
  void 0 == b || "" == b
    ? setElDisable("nextevebutton", !0, 1)
    : setElDisable("nextevebutton", !1, 1);
}

//获取类型
function getTypeString(b) {
  for (var c = "", a = 0; a < eventType.length; a++)
    if (b == eventType[a][0]) {
      c = eventType[a][1];
      break;
    }
  return c;
}

//获取端口的IP信息
function getPortIpaddrString(b, c) {
  var a = getPortString(b),
    d = "-";
  1 == b &&
    ((d = [
      c.substring(0, 2),
      c.substring(2, 4),
      c.substring(4, 6),
      c.substring(6, 8),
    ]),
    (d =
      parseInt(d[0], 16).toString() +
      "." +
      parseInt(d[1], 16).toString() +
      "." +
      parseInt(d[2], 16).toString() +
      "." +
      parseInt(d[3], 16).toString()),
    "0.0.0.0" == d && (d = "-"));
  return userStr[0] + a + "\r\n" + userStr[1] + d;
}
//获取端口信息
function getPortString(b) {
  for (var c = "-", a = 0; a < eventport.length; a++)
    if (b == eventport[a][0]) {
      c = eventport[a][1];
      break;
    }
  return c;
}
// 加载访问Log页面
function onloadAccLog() {
  saveCurrentPage("inmenu5", "AccLog");
  mainTimerCycle = getStorage("mainTimerCycle", mainTimerCycle);
  monAccStartStop(1);
  logoutBtnDisable(!1);
}

function unloadAccLog() {
  monAccStartStop(0);
  logoutBtnDisable(!0);
}

//5秒更新一次
function onAccLogTimerStart() {
  onAccLogTimerStop();
  updateAccLog();
  timerID_Acc = setInterval(updateAccLog, 1e3 * mainTimerCycle);
}
//禁用更新
function onAccLogTimerStop() {
  timerID_Acc && clearInterval(timerID_Acc);
}

function updateAccLog() {
  GetAccLog();
}

function monAccStartStop(b) {
  1 == b ? onAccLogTimerStart() : onAccLogTimerStop();
}

//获取访问信息
function GetAccLog() {
  try {
    onTimerStopForW10(),
      (accXhr = new XMLHttpRequest()),
      accXhr.open("POST", "/cgi/GetAccLog.cgi", !0),
      accXhr.setRequestHeader(HDTYP, HDVAL),
      (accXhr.onreadystatechange = function () {
        GetAccLog_Response(accXhr);
      }),
      accXhr.send(null);
  } catch (b) {
    console.log(b.message);
  }
}
//解析访问Log
function GetAccLog_Response(b) {
  try {
    if (4 != b.readyState || 200 != b.status)
      3 == b.readyState && onTimerStartForW10();
    else {
      var a = JSON.parse(b.response),
        d = a.LOG.length;
      b = [];
      if ("0001" === a.RET) monAccStartStop(0);
      else {
        for (var c = 0; c < d; c++) {
          b[c] = [];
          var k,
            l,
            f = a.LOG[c][0].split("-"),
            g = a.LOG[c][1].split(":");
          k =
            ("0000" + parseInt(f[0], 16)).slice(-4) +
            "-" +
            ("00" + parseInt(f[1], 16)).slice(-2) +
            "-" +
            ("00" + parseInt(f[2], 16)).slice(-2);
          l =
            ("00" + parseInt(g[0], 16)).slice(-2) +
            ":" +
            ("00" + parseInt(g[1], 16)).slice(-2) +
            ":" +
            ("00" + parseInt(g[2], 16)).slice(-2);
          b[c][0] = k + "\r\n" + l;
          b[c][1] = changeCharCode(a.LOG[c][2]);
          b[c][2] =
            getAccStr(a.LOG[c][3]) + "\r\n" + changeCharCode(a.LOG[c][4]);
          var h = a.LOG[c][5];
          "0" == h && (h = "----");
          b[c][3] = h;
          b[c][4] = getPrtclStr(a.LOG[c][6]);
          var e = [
            a.LOG[c][7].substring(0, 2),
            a.LOG[c][7].substring(2, 4),
            a.LOG[c][7].substring(4, 6),
            a.LOG[c][7].substring(6, 8),
          ];
          b[c][5] =
            parseInt(e[0], 16).toString() +
            "." +
            parseInt(e[1], 16).toString() +
            "." +
            parseInt(e[2], 16).toString() +
            "." +
            parseInt(e[3], 16).toString();
        }
        for (a = 0; a < b.length; a++)
          setInTxt("number" + (a + 1), b.length - a, 1),
            setInTxt("date" + (a + 1), b[a][0], 1),
            setInTxt("user" + (a + 1), b[a][1], 1),
            setInTxt("action" + (a + 1), b[a][2], 1),
            setInTxt("connNo" + (a + 1), b[a][3], 1),
            setInTxt("protocol" + (a + 1), b[a][4], 1),
            setInTxt("ipaddress" + (a + 1), b[a][5], 1);
        for (; 128 > a; a++)
          setInTxt("number" + (a + 1), "", 1),
            setInTxt("date" + (a + 1), "", 1),
            setInTxt("user" + (a + 1), "", 1),
            setInTxt("action" + (a + 1), "", 1),
            setInTxt("connNo" + (a + 1), "", 1),
            setInTxt("protocol" + (a + 1), "", 1),
            setInTxt("ipaddress" + (a + 1), "", 1);
      }
    }
  } catch (m) {
    console.log(m.message);
  }
}
//
//转为Unicode字符集
function changeCharCode(b) {
  for (var a = "", d = 0; d < b.length; d += 2)
    a += String.fromCharCode(parseInt(b.slice(d, d + 2), 16));
  return a;
}

//PLC监视读取与写入
var writeXhr,
  RUN_COLOR = "rgb(124,252,0)",
  BACK_COLOR = "rgb(221,221,221)",
  WATCH_BTN_SELECT = "rgb(255,255,255)",
  WATCH_BTN_SELECT_BG = "rgb(52,146,232)",
  BIT_ON_COLOR = "rgb(0,102,255)",
  MAX_DISP_LINE = 32,
  BIT_POINT_8 = 8,
  BIT_POINT_10 = 10,
  BIT_POINT_4 = 4,
  nowDisplay = 1,
  c_t_DataArray = [],
  tstValue,
  showTest = 0,
  nextClk = 0,
  entChk = 0,
  watchStatus = 0,
  monStatus = 0,
  croppedstr,
  flushArray = [],
  writing = 0,
  xhrTimeout_msec = 1e4,
  WATCH_ERR = 16,
  WATCH_OK = 1,
  WATCH_OKE = 2,
  WATCH_AOK = 3;
  //界面加载，从网页会话内容获取之前监视区域参数
function onloadDevBatMon() {
  try {
    saveCurrentPage("inmenu2", "DevBatMon");
    mainTimerCycle = getStorage("mainTimerCycle", mainTimerCycle);
    mainSetType = getStorage("mainSetType", 0);
    mainDevType = getStorage("mainDevType", "D");
    mainDevAddress = getStorage("mainDevAddress", "0");
    mainBfmUnitNo = getStorage("mainBfmUnitNo", "1");
    mainBfmAddress = getStorage("mainBfmAddress", "0");
    mainDataType = getStorage("mainDataType", 2);
    mainInputType = getStorage("mainInputType", "10");
    mainBitsortSel = getStorage("mainBitsortSel", 0);
    isBatMonitor = 0;
    monStatus = 1;
    mainMultiPoint = getStorage("mainMultiPoint", 1);
    nextClk = showTest = 0;
    mainBlSelect = getStorage("mainBlSelect", "-");
    mainBlAddress = getStorage("mainBlAddress", "0");
    setElIdVal("devselect", mainDevType, 1);
    setElIdVal("devaddress", mainDevAddress, 1);
    setElIdVal("moduleNo", mainBfmUnitNo, 1);
    setElIdVal("memaddress", mainBfmAddress, 1);
    setElIdVal("datatype", mainDataType, 1);
    setElIdVal("formattype", mainInputType, 1);
    setElIdVal("bitsortpull", mainBitsortSel, 1);
    setElIdVal("bl_select", mainBlSelect, 1);
    setElIdVal("bl_address", mainBlAddress, 1);
    var a = getElIdObj("bl_view", 1),
      b = getElIdObj("bl_bkslush", 1),
      c = getElIdObj("bl_address", 1);
    "BL" == mainDevType
      ? ((a.style.visibility = "visible"),
        "S" == mainBlSelect
          ? ((b.style.visibility = "visible"), (c.style.visibility = "visible"))
          : ((b.style.visibility = "hidden"), (c.style.visibility = "hidden")))
      : ((a.style.visibility = "hidden"),
        (b.style.visibility = "hidden"),
        (c.style.visibility = "hidden"));
    var d;
    0 == mainSetType
      ? ((getElIdObj("radio001", 1).checked = !0),
        (d = mainDevType),
        setElDisable("devselect", !1, 1),
        setElDisable("devaddress", !1, 1),
        setElDisable("moduleNo", !0, 1),
        setElDisable("memaddress", !0, 1),
        setBkCol("memaddress", "", 1),
        setElDisable("bl_view", !1, 1),
        setElDisable("bl_select", !1, 1),
        setElDisable("bl_address", !1, 1))
      : 1 == mainSetType &&
        ((getElIdObj("radio002", 1).checked = !0),
        (d = "D"),
        setElDisable("devselect", !0, 1),
        setElDisable("devaddress", !0, 1),
        setElDisable("moduleNo", !1, 1),
        setElDisable("memaddress", !1, 1),
        setBkCol("devaddress", "", 1),
        setElDisable("bl_view", !0, 1),
        setElDisable("bl_select", !0, 1),
        setElDisable("bl_address", !0, 1),
        setBkCol("bl_address", "", 1));
    changeMonitorSelectBox();
    setElIdVal("datatype", mainDataType, 1);
    changeFmtSelectStyle(
      getElIdObj("datatype", 1),
      getElIdObj("formattype", 1)
    );
    0 == getElIdObj("formattype", 1).disabled &&
      setElIdVal("formattype", mainInputType, 1);
    var e = getElIdObj("partdisptype", 1);
    setMultiSelect(e, checkDevtype(d + "0"));
    getDisplayType(d);
    a = mainMultiPoint;
    a > e.length && (a = 0);
    setElIdVal("partdisptype", a, 1);
    setElIdVal("bitsortpull", mainBitsortSel, 1);
    0 == mainSetType
      ? (fixDevAddress(),
        (getElIdObj("devaddress", 1).style.backgroundColor = ""),
        "S" == mainBlSelect &&
          (getElIdObj("bl_address", 1).style.backgroundColor = ""))
      : 1 == mainSetType &&
        (fixBfmAddress(),
        (getElIdObj("memaddress", 1).style.backgroundColor = ""));
    onchangeMultiSelect();
  } catch (f) {
    console.log(f.message);
  }
  logoutBtnDisable(!1);
}
//5秒一次更新内容
function onBatTimerStart() {
  onBatTimerStop();
  updateBatTable();
  timerID_bat = setInterval(updateBatTable, 1e3 * mainTimerCycle);
}
function onBatTimerStop() {
  timerID_bat && clearInterval(timerID_bat);
}
function updateBatTable() {
  flushArray = [];
  viewarea.document.getElementById("loading-win").style.display = "block";
  ReadDeviceBlock([], 0);
}
function onBatStartStop() {
  var a = getElIdVal("devaddress", 1),
    b = getElIdVal("memaddress", 1);
  1 == isBatMonitor
    ? monBatStartStop(0)
    : getElIdObj("radio001", 1).checked
    ? "" == a
      ? alertMessage("mon_err01", 2)
      : fixDevAddress()
    : getElIdObj("radio002", 1).checked &&
      ("" == b ? alertMessage("mon_err02", 2) : fixBfmAddress());
}
function monBatStartStop(a) {
  1 == a
    ? ((isBatMonitor = 1),
      setElDisable("prevbutton", !1, 1),
      setElDisable("nextbutton", !1, 1),
      restrictUnderZero(),
      onBatTimerStart())
    : (onBatTimerStop(),
      setElDisable("prevbutton", !0, 1),
      setElDisable("nextbutton", !0, 1),
      (isBatMonitor = 0));
  setStorage("isBatMonitor", isBatMonitor);
}
function onchangeRadioDevSet() {
  getElIdObj("radio001", 1).checked
    ? (setElDisable("devselect", !1, 1),
      setElDisable("devaddress", !1, 1),
      setElDisable("moduleNo", !0, 1),
      setElDisable("memaddress", !0, 1),
      setBkCol("memaddress", "", 1),
      setElDisable("bl_view", !1, 1),
      setElDisable("bl_select", !1, 1),
      setElDisable("bl_address", !1, 1),
      (onloadFirst = !0),
      fixDevAddress())
    : getElIdObj("radio002", 1).checked &&
      (setElDisable("devselect", !0, 1),
      setElDisable("devaddress", !0, 1),
      setElDisable("moduleNo", !1, 1),
      setElDisable("memaddress", !1, 1),
      setBkCol("devaddress", "", 1),
      setElDisable("bl_view", !0, 1),
      setElDisable("bl_select", !0, 1),
      setElDisable("bl_address", !0, 1),
      setBkCol("bl_address", "", 1),
      (onloadFirst = !0),
      fixBfmAddress());
}
function onchangeDataType() {
  var a = getElIdObj("datatype", 1);
  mainDataType = a.value;
  setStorage("mainDataType", mainDataType);
  var b = getElIdObj("formattype", 1);
  changeFmtSelectStyle(a, b);
  a = getDisplayType(0 == mainSetType ? mainDevType : "D");
  setBitSortPull(getElIdObj("bitsortpull", 1), a);
  setElIdVal("bitsortpull", mainBitsortSel, 1);
  makeBitDisplayHead(a);
  changeDisplay(a);
  monBatStartStop(isBatMonitor);
}
function onchangeNumType() {
  mainInputType = getElIdVal("formattype", 1);
  setStorage("mainInputType", mainInputType);
  monBatStartStop(isBatMonitor);
}
function onStartAddress(a) {
  if (13 == a) {
    if (
      1 == isBatMonitor &&
      mainDevType == getElIdVal("devselect", 1) &&
      mainDevAddress == getElIdVal("devaddress", 1) &&
      0 == mainSetType &&
      getElIdObj("radio001", 1).checked
    )
      if (
        ((getElIdObj("devaddress", 1).style.backgroundColor = ""),
        mainBlSelect == getElIdVal("bl_select", 1))
      ) {
        if (mainBlAddress == getElIdVal("bl_address", 1)) {
          getElIdObj("bl_address", 1).style.backgroundColor = "";
          return;
        }
      } else return;
    onloadFirst = !0;
    fixDevAddress();
  }
}
function onBfmUnitNo() {
  if (1 == getElIdObj("radio002", 1).checked) {
    var a = getElIdObj("memaddress", 1);
    a.focus();
    a.select();
    onloadFirst = !0;
    fixBfmAddress();
  }
}
function onBfmStartAddress(a) {
  13 == a &&
    (1 == isBatMonitor &&
    mainBfmUnitNo == getElIdVal("moduleNo", 1) &&
    mainBfmAddress == getElIdVal("memaddress", 1) &&
    1 == mainSetType &&
    getElIdObj("radio002", 1).checked
      ? (getElIdObj("memaddress", 1).style.backgroundColor = "")
      : ((onloadFirst = !0), fixBfmAddress()));
}
function setDevBatMon(a) {
  var b, c;
  if (0 == mainSetType) {
    b = mainDevType;
    c = mainDevAddress;
    var d = getElIdVal("bl_select", 1);
    "BL" == b && "S" == d && (c = mainBlAddress);
  } else (b = "U" + mainBfmUnitNo + "\\G"), (c = mainBfmAddress);
  if ("" != c) {
    var d = getNumType(b),
      e = convertDecNum(b, c);
    startPoint != e && (c = startPoint.toString(d));
    for (var f = (e = 0); f < MAX_DISP_LINE; f++) {
      var g = c,
        h = getDisplayType(0 == mainSetType ? b : "D"),
        g = parseInt(g, d),
        g = (parseInt(8 == h || 10 == h || 4 == h || 18 == h ? e : f, 10) + g)
          .toString(d)
          .toUpperCase(),
        k;
      1 == mainSetType
        ? ((k = e >= a ? "" : b + g),
          4 == h
            ? (setInTxt("dw_multi4dev" + (f + 1), k, 1), (e += 8))
            : 18 == h
            ? (setInTxt("w_multi8dev" + (f + 1), k, 1), (e += 8))
            : setInTxt("dev" + (f + 1), k, 1))
        : (0 == g.search(/[A-F]/) && (g = "0" + g),
          (k = e >= a ? "" : b + g),
          "BL" == mainDevType &&
            "S" == mainBlSelect &&
            (k = e >= a ? "" : b + mainDevAddress + "\\S" + g),
          8 == h
            ? (setInTxt("bit8dev" + (f + 1), k, 1), (e += 8))
            : 10 == h
            ? (setInTxt("bit10dev" + (f + 1), k, 1), (e += 10))
            : 3 == h
            ? setInTxt("c_t_dev" + (f + 1), k, 1)
            : 4 == h
            ? (setInTxt("dw_multi4dev" + (f + 1), k, 1),
              (e = "W" == getDevType(b) ? e + 8 : e + 4))
            : 18 == h
            ? (setInTxt("w_multi8dev" + (f + 1), k, 1), (e += 8))
            : setInTxt("dev" + (f + 1), k, 1));
    }
  }
}
//获取数据类型的对应数字编号
function getNumType(a) {
  switch (a) {
    case "X":
    case "Y":
      a = 8;
      break;
    case "B":
    case "SB":
    case "W":
    case "SW":
      a = 16;
      break;
    case "D":
    case "M":
    case "L":
    case "F":
    case "TS":
    case "STS":
    case "CS":
    case "LCS":
    case "SM":
    case "SD":
    case "R":
    case "Z":
    case "LZ":
    case "TC":
    case "TN":
    case "STC":
    case "STN":
    case "CC":
    case "CN":
    case "LCC":
    case "LCN":
    case "BL":
      a = 10;
      break;
    default:
      a = 10;
  }
  return a;
}
//获取数据类型的对应内存形式
function getDevType(a) {
  switch (a) {
    case "X":
    case "Y":
    case "M":
    case "L":
    case "B":
    case "F":
    case "S":
    case "SB":
    case "SM":
    case "BL":
      a = "B";
      break;
    case "LZ":
      a = "D";
      break;
    default:
      a = "W";
  }
  return a;
}
//获取显示类型
function getDisplayType(a) {
  switch (a) {
    case "X":
    case "Y":
    case "B":
    case "SB":
      a = 8;
      break;
    case "M":
    case "L":
    case "F":
    case "S":
    case "SM":
    case "BL":
      a = 10;
      break;
    case "D":
    case "W":
    case "SW":
    case "SD":
    case "R":
    case "Z":
      a =
        1 == getElIdVal("partdisptype", 1)
          ? 1
          : 3 == mainDataType || 2 == mainDataType
          ? 18
          : 4;
      break;
    case "LZ":
      a = 1 == getElIdVal("partdisptype", 1) ? 1 : 4;
      break;
    case "C":
    case "T":
    case "ST":
    case "LC":
      a = 3;
      break;
    default:
      a = 1;
  }
  return a;
}
//获取16进制下16+16的32位字符串
function makeDoubleStr(a, b) {
  a || (a = "0000");
  b || (b = "0000");
  return (
    ("000" + a).slice(-4).toUpperCase() + ("000" + b).slice(-4).toUpperCase()
  );
}
//调整显示
function getFixValue(a, b) {
  var c;
  if (10 == getElIdVal("formattype", 1)) c = changeIntNum(a, b);
  else if (16 == getElIdVal("formattype", 1))
    if ("2" == b || "3" == b) c = "H" + ("000" + a).slice(-4).toUpperCase();
    else {
      if ("4" == b || "5" == b)
        c = "H" + ("0000000" + a).slice(-8).toUpperCase();
    }
  else c = a;
  return c;
}
//PLC块读取
function ReadDeviceBlock(a, b) {
  var c, d;
  c = getTargetIDHead(nowDisplay);
  onTimerStopForW10();
  c = c[0];
  d = getStartDev(b);
  if ("" == d)
    viewarea.document.getElementById("loading-win").style.display = "none";
  else {
    var e = mainDevType;
    1 == mainSetType && (e = "D");
    if (3 == getDisplayType(e)) ReadDeviceBlock_Continue([], 0);
    else {
      d = changeDevItem(d);
      c =
        "NUM=" +
        encodeURIComponent(c.toString(16)) +
        "&DEV=" +
        encodeURIComponent(d);
      try {
        (a[b] = new XMLHttpRequest()),
          a[b].open("POST", "/cgi/RdDevBlk.cgi", !0),
          a[b].setRequestHeader(HDTYP, HDVAL),
          (a[b].timeout = xhrTimeout_msec),
          (a[b].ontimeout = function (a) {
            console.error("xhr.ontimeout");
            viewarea.document.getElementById("loading-win").style.display =
              "none";
          }),
          (a[b].onerror = function (a) {
            console.error("xhr.onerror : [ " + a.type + " ] ");
            viewarea.document.getElementById("loading-win").style.display =
              "none";
          }),
          (a[b].onreadystatechange = function () {
            ReadDeviceBlock_Response(a, b);
          }),
          a[b].send(c);
      } catch (f) {
        (viewarea.document.getElementById("loading-win").style.display =
          "none"),
          console.log(f.message);
      }
    }
  }
}
//XHR块读取响应处理
function ReadDeviceBlock_Response(a, b) {
  if (4 != a[b].readyState || 200 != a[b].status)
    3 == a[b].readyState && onTimerStartForW10();
  else {
    var c = JSON.parse(a[b].response),
      d = 1;
    4 == nowDisplay
      ? (d = "LZ" == mainDevType ? 4 : 8)
      : 18 == nowDisplay && (d = 8);
    if ("0000" === c.RET) {
      Array.prototype.push.apply(flushArray, c.DATA);
      var e = d - 1;
      c.DATA.length < MAX_DISP_LINE && (e = b);
      b != e
        ? ReadDeviceBlock(a, b + 1)
        : (1 == d
            ? putReadDataBat(flushArray, parseInt(c.NUM, 16))
            : putReadDataBat(flushArray, flushArray.length),
          (nextClk = 0),
          (viewarea.document.getElementById("loading-win").style.display =
            "none"));
    } else
      "0001" === c.RET
        ? monBatStartStop(0)
        : "4031" === c.RET &&
          (1 == nextClk
            ? (putReadDataBat(flushArray, flushArray.length),
              (nextClk = 0),
              1 > flushArray.length && changePageBat(-1))
            : (putReadDataBat(flushArray, flushArray.length), (nextClk = 0)),
          setElDisable("nextbutton", !0, 1)),
        (viewarea.document.getElementById("loading-win").style.display =
          "none");
  }
}
//当选中设备区域改变
function onchangeDevSelect() {
  var a = getElIdObj("devaddress", 1);
  a.focus();
  a.select();
  onloadFirst = !0;
  var a = getElIdVal("devselect", 1),
    b = getElIdObj("bl_view", 1),
    c = getElIdObj("bl_bkslush", 1),
    d = getElIdObj("bl_address", 1);
  "BL" == a
    ? ((b.style.visibility = "visible"),
      setElIdVal("bl_select", mainBlSelect, 1),
      onchangeBlSelect())
    : ((b.style.visibility = "hidden"),
      (c.style.visibility = "hidden"),
      (d.style.visibility = "hidden"),
      fixDevAddress());
}
//加载注册地址的监视画面
function onloadDevRegMon() {
  getlocalStorageArray("mainDevRegData");
  watchStatus = isRegMonitor = 1;
  showTest = 0;
  saveCurrentPage("inmenu3", "DevRegMon");
  mainTimerCycle = getStorage("mainTimerCycle", mainTimerCycle);
  viewPageNum = parseInt(getStorage("viewPageNum", 1));
  var a = mainDevRegData;
  changePageReg(viewPageNum);
  for (var b = 0; b < a.length; b++) {
    var c = b + 1;
    if ("" != a[b][0]) {
      var d = getElIdObj("dev" + c, 1);
      d.value = a[b][0];
      if (WATCH_AOK & a[b][3]) {
        var d = checkDevtype(d.value),
          e = getElIdObj("devfmt" + c, 1),
          f = getElIdObj("devsize" + c, 1);
        WATCH_OKE == a[b][3] && "W" == d && (d = "WS");
        setRegMonSelect(e, f, d);
        e.value = a[b][1];
        f.value = a[b][2];
      } else
        WATCH_ERR == a[b][3] && (d.style.backgroundColor = "rgb(255,192,192)");
      updateSelChange(c);
    }
  }
  monRegStartStop(isRegMonitor);
  logoutBtnDisable(!1);
}
function onChangeText(a) {
  0 == entChk && onEnterText(13, a);
  entChk = 0;
}
function setRegMonSelect(a, b, c) {
  var d = a.options;
  b = b.options;
  d.length = 0;
  b.length = 0;
  "B" == c
    ? ((d[0] = new Option(getRadixTypeStr(2))),
      (d[0].value = 2),
      (b[0] = new Option(getDataTypeStr(1))),
      (b[0].value = 1))
    : "S" == c
    ? ((d[0] = new Option(getRadixTypeStr(10))),
      (d[0].value = 10),
      (d[1] = new Option(getRadixTypeStr(16))),
      (d[1].value = 16),
      (b[0] = new Option(getDataTypeStr(3))),
      (b[0].value = 3))
    : "D" == c
    ? ((d[0] = new Option(getRadixTypeStr(10))),
      (d[0].value = 10),
      (d[1] = new Option(getRadixTypeStr(16))),
      (d[1].value = 16),
      (b[0] = new Option(getDataTypeStr(5))),
      (b[0].value = 5),
      (b[1] = new Option(getDataTypeStr(4))),
      (b[1].value = 4),
      (b[1].selected = !0))
    : "DS" == c
    ? ((d[0] = new Option(getRadixTypeStr(10))),
      (d[0].value = 10),
      (d[1] = new Option(getRadixTypeStr(16))),
      (d[1].value = 16),
      (b[0] = new Option(getDataTypeStr(5))),
      (b[0].value = 5),
      (b[1] = new Option(getDataTypeStr(4))),
      (b[1].value = 4),
      (b[2] = new Option(getDataTypeStr(6))),
      (b[2].value = 6),
      (b[1].selected = !0))
    : "W" == c
    ? ((d[0] = new Option(getRadixTypeStr(10))),
      (d[0].value = 10),
      (d[1] = new Option(getRadixTypeStr(16))),
      (d[1].value = 16),
      (b[0] = new Option(getDataTypeStr(3))),
      (b[0].value = 3),
      (b[1] = new Option(getDataTypeStr(5))),
      (b[1].value = 5),
      (b[2] = new Option(getDataTypeStr(2))),
      (b[2].value = 2),
      (b[3] = new Option(getDataTypeStr(4))),
      (b[3].value = 4),
      (b[4] = new Option(getDataTypeStr(6))),
      (b[4].value = 6),
      (b[2].selected = !0))
    : "WS" == c &&
      ((d[0] = new Option(getRadixTypeStr(10))),
      (d[0].value = 10),
      (d[1] = new Option(getRadixTypeStr(16))),
      (d[1].value = 16),
      (b[0] = new Option(getDataTypeStr(3))),
      (b[0].value = 3),
      (b[1] = new Option(getDataTypeStr(2))),
      (b[1].value = 2),
      (b[1].selected = !0));
  d[0].selected = !0;
  a.disabled = !1;
}
//输入内容验证
function onEnterText(a, b) {
  if (13 == a) {
    var c,
      d = getElIdObj("dev" + b, 1),
      e = [];
    entChk = 1;
    checkWacthDev(b);
    if ("" == d.value) setEmptyItemReg(b);
    else {
      c = d.value.toUpperCase();
      var f = checkDevtype(c, e);
      "ERR" == f
        ? setErrorItemReg(b)
        : ((c = e[0]), (d.value = e[0]), checkInputDevice(c, b, f));
    }
  }
}
//停止监视1或监视2
function onRegStartStop() {
  0 == isRegMonitor ? monRegStartStop(1) : monRegStartStop(0);
}
//网页会话记录isRegMonitor值
function monRegStartStop(a) {
  1 == a
    ? ((isRegMonitor = 1), onRegTimerStart())
    : (onRegTimerStop(), (isRegMonitor = 0));
  setStorage("isRegMonitor", isRegMonitor);
}
//开始监视
function onRegTimerStart() {
  onRegTimerStop();
  updateRegTable();
  timerID_reg = setInterval(updateRegTable, 1e3 * mainTimerCycle);
}
//按时刷新
function onRegTimerStop() {
  timerID_reg && clearInterval(timerID_reg);
}
//读取并渲染
function updateRegTable() {
  ReadDeviceRandom();
}
function updateSelChange(a) {
  var b = getElIdObj("devfmt" + a, 1);
  a = getElIdObj("devsize" + a, 1);
  changeFmtSelectStyle(a, b);
}
//浏览器本地存储注册监视画面已登录的地址
function saveWatchTable(a) {
  var b = a - 1;
  mainDevRegData[b][0] = getElIdVal("dev" + a, 1);
  mainDevRegData[b][1] = getElIdVal("devfmt" + a, 1);
  mainDevRegData[b][2] = getElIdVal("devsize" + a, 1);
  a = JSON.stringify(mainDevRegData);
  setlocalStorage("mainDevRegData", a);
}
function changeNumTypeList(a) {
  saveWatchTable(a);
  monRegStartStop(isRegMonitor);
}
function changeDataTypeList(a) {
  updateSelChange(a);
  saveWatchTable(a);
  monRegStartStop(isRegMonitor);
}
//读取不连续内存地址
function ReadDeviceRandom() {
  var a = [],
    b = 0,
    c,
    d,
    e = 0;
  onTimerStopForW10();
  try {
    2 == viewPageNum && (b = MAX_DISP_LINE);
    for (var f = mainDevRegData, g = 0; g < MAX_DISP_LINE; g++)
      (d = g + b), WATCH_AOK & f[d][3] && ((a[e] = f[d]), e++);
    var h = "NUM=" + e.toString(16) + "&";
    if (1 > e) h = "";
    else {
      for (b = 0; b < e; b++) {
        c = b + 1;
        var k = a[b],
          m,
          l;
        m = changeDevItem(k[0]);
        h += "DEV" + c + "=" + encodeURIComponent(m) + "&";
        switch (k[2]) {
          case "1":
            l = "B";
            break;
          case "4":
          case "5":
          case "6":
            l = "D";
            break;
          default:
            l = "W";
        }
        h += "TYP" + c + "=" + l;
        b < e - 1 && (h += "&");
      }
      var p;
      p = new XMLHttpRequest();
      p.open("POST", "/cgi/RdDevRnd.cgi", !0);
      p.setRequestHeader(HDTYP, HDVAL);
      p.onreadystatechange = function () {
        ReadDeviceRandom_Response(p);
      };
      p.send(h);
    }
  } catch (n) {
    console.log(n.message);
  }
}
//解析返回的值
function ReadDeviceRandom_Response(a) {
  try {
    if (4 != a.readyState || 200 != a.status)
      3 == a.readyState && onTimerStartForW10();
    else {
      getElIdObj("watchtable", 1);
      var b = JSON.parse(a.response);
      "0000" === b.RET
        ? putReadData(b.DATA)
        : "0001" === b.RET
        ? monRegStartStop(0)
        : (monRegStartStop(0), alertMessage(b.RET, 2));
    }
  } catch (c) {
    console.log(c.message);
  }
}
//转换浮点数据
function changeFloatNum(a) {
  var b = new ArrayBuffer(16),
    b = new DataView(b);
  a = ("0000000" + a).slice(-8).toUpperCase();
  setDataView(parseInt(a, 16), "5", b, !1);
  a = getDataView10("6", b, !1);
  a = rangeCheckFloat(a);
  return 0 == a.length ? "--" : a.toPrecision(7);
}
function changeIntNum(a, b) {
  var c = new ArrayBuffer(16),
    c = new DataView(c);
  setDataView(parseInt(a, 16), b, c, !1);
  return getDataView10(b, c, !1);
}
//dataview.setUint16(byteOffset, value [, littleEndian是否低位编址，默认BigEndian 即 ABCD12 -> AB CD 12]) 进制与格式
function setDataView(a, b, c, d) {
  switch (b) {
    case "1":
      c.setUint8(0, a, d);
      break;
    case "3":
      c.setUint16(0, a, d);
      break;
    case "4":
      c.setInt32(0, a, d);
      break;
    case "5":
      c.setUint32(0, a, d);
      break;
    case "6":
      c.setFloat32(0, a, d);
      break;
    case "7":
      c.setUint16(0, a, d);
      break;
    default:
      c.setInt16(0, a, d);
  }
}
function getDataView10(a, b, c) {
  switch (a) {
    case "3":
      a = b.getUint16(0, c);
      break;
    case "4":
      a = b.getInt32(0, c);
      break;
    case "5":
      a = b.getUint32(0, c);
      break;
    case "6":
      a = b.getFloat32(0, c);
      break;
    default:
      a = b.getInt16(0, c);
  }
  return a;
}
function getDataView16(a, b, c) {
  switch (a) {
    case "4":
    case "5":
    case "6":
      a = b.getUint32(0, c).toString(16);
      break;
    default:
      a = b.getUint16(0, c).toString(16);
  }
  return a;
}
//字符串处理
function rangeCheckString(a) {
  for (var b = a, c = !1, d = 0; d < b.length; d++)
    if (4 <= escape(b.charAt(d)).length) {
      if (2 <= b.length) return "";
      c = !0;
    }
  return c
    ? ""
    : (a = parseInt(
        a.charCodeAt(1).toString(16) + a.charCodeAt(0).toString(16),
        16
      ));
}
function changeAsciiStr(a) {
  var b = ("000" + a).slice(-4);
  a = "";
  var c = b.slice(0, 2),
    b = b.slice(2, b.length);
  parseInt(c, 16);
  var d = parseInt(b, 16);
  (129 <= d && 159 >= d) || (224 <= d && 252 >= d)
    ? (a = String.fromCharCode(parseInt(b + c, 16)))
    : ((a += String.fromCharCode("0x" + b)),
      (a += String.fromCharCode("0x" + c)));
  return a;
}
//显示读取数据parseInt(, 16) changeFloatNum() changeAsciiStr()
function putReadData(a) {
  var b,
    c,
    d,
    e = 0,
    f,
    g,
    h = mainDevRegData,
    k = 0;
  2 == viewPageNum && (e = MAX_DISP_LINE);
  for (var m = 0; m < MAX_DISP_LINE; m++)
    if (((f = m + e + 1), 0 != (WATCH_AOK & h[m + e][3]))) {
      b = getElIdVal("devfmt" + f, 1);
      c = getElIdVal("devsize" + f, 1);
      d = getElIdObj("val" + f, 1);
      switch (c) {
        case "1":
          g = parseInt(a[k], 16);
          setColBitMon(f, g);
          2 == b && (g = g.toString(2));
          break;
        case "2":
          g = parseInt(a[k], 16);
          10 == b
            ? (g = changeIntNum(a[k], "2"))
            : 16 == b &&
              (g = "H" + ("000" + g.toString(16)).slice(-4).toUpperCase());
          break;
        case "3":
          g = parseInt(a[k], 16);
          10 == b
            ? (g = changeIntNum(a[k], "3"))
            : 16 == b &&
              (g = "H" + ("000" + g.toString(16)).slice(-4).toUpperCase());
          break;
        case "4":
          g = parseInt(a[k], 16);
          10 == b
            ? (g = changeIntNum(a[k], "4"))
            : 16 == b &&
              (g = "H" + ("0000000" + g.toString(16)).slice(-8).toUpperCase());
          break;
        case "5":
          g = parseInt(a[k], 16);
          10 == b
            ? (g = changeIntNum(a[k], "5"))
            : 16 == b &&
              (g = "H" + ("0000000" + g.toString(16)).slice(-8).toUpperCase());
          break;
        case "6":
          g = changeFloatNum(a[k]);
          break;
        case "7":
          g = changeAsciiStr(a[k]);
      }
      1 != c && setColBitMon(f, 0);
      d.value = g;
      k++;
    }
}
//鼠标移过颜色区分
function onRegMouseOver(a) {
  var b, c;
  devsize = getElIdVal("devsize" + a, 1);
  val = getElIdVal("val" + a, 1);
  c = LINE_HOVER_STRCOLOR;
  b =
    (1 == devsize && 1 == val) || 0 == a % 2
      ? LINE_HOVER_COLOR2
      : LINE_HOVER_COLOR;
  setStlCol("devnametd" + a, c, 1);
  setStlCol("dev" + a, c, 1);
  setStlCol("disptypetd" + a, c, 1);
  setStlCol("devfmt" + a, c, 1);
  setStlCol("datatypetd" + a, c, 1);
  setStlCol("devsize" + a, c, 1);
  setStlCol("readvaluetd" + a, c, 1);
  setStlCol("val" + a, c, 1);
  setBkCol("devnametd" + a, b, 1);
  0 == (WATCH_ERR & mainDevRegData[a - 1][3]) && setBkCol("dev" + a, b, 1);
  setBkCol("disptypetd" + a, b, 1);
  setBkCol("devfmt" + a, b, 1);
  setBkCol("datatypetd" + a, b, 1);
  setBkCol("devsize" + a, b, 1);
  setBkCol("readvaluetd" + a, b, 1);
  setBkCol("val" + a, b, 1);
}
function onRegMouseOut(a) {
  devsize = getElIdVal("devsize" + a, 1);
  val = getElIdVal("val" + a, 1);
  1 == devsize && 1 == val
    ? setLineColorMon(a, BIT_ON_COLOR, WATCH_BTN_SELECT)
    : (setStlCol("devnametd" + a, "", 1),
      setStlCol("dev" + a, "", 1),
      setStlCol("disptypetd" + a, "", 1),
      setStlCol("devfmt" + a, "", 1),
      setStlCol("datatypetd" + a, "", 1),
      setStlCol("devsize" + a, "", 1),
      setStlCol("readvaluetd" + a, "", 1),
      setStlCol("val" + a, "", 1),
      setBkCol("devnametd" + a, "", 1),
      0 == (WATCH_ERR & mainDevRegData[a - 1][3]) && setBkCol("dev" + a, "", 1),
      setBkCol("disptypetd" + a, "", 1),
      setBkCol("devfmt" + a, "", 1),
      setBkCol("datatypetd" + a, "", 1),
      setBkCol("devsize" + a, "", 1),
      setBkCol("readvaluetd" + a, "", 1),
      setBkCol("val" + a, "", 1));
}
//initial device test window
function InitDevTestWnd() {
  var a = getElIdObj("datatype", 2),
    b = getElIdObj("inputtype", 2);
  setInTxt("device", tstDev, 2);
  cpySelectItem(tstType, a);
  cpySelectItem(tstFmt, b);
  onchangeTstType();
  a = tstVal.value;
  "16" == b.value && (a = a.replace("H", ""));
  setElIdVal("setvalue", a, 2);
  getElIdObj("setvalue", 2).focus();
  getElIdObj("setvalue", 2).select();
  showTest = 1;
  showMessage(1);
}
//点击值写入时弹窗确认
function onSetBtn() {
  var a = getElIdObj("setvalue", 2),
    b = getErrorStr("msg_rangeover");
  value = convertInputVal();
  if (0 == value.length) setInTxt("message", b, 2), showMessage(3);
  else {
    var b = getInTxt("device", 2),
      c = getErrorStr("msg_device"),
      d = getErrorStr("msg_to"),
      e = getErrorStr("msg_towrite"),
      f = getErrorStr("msg_warning"),
      g = getElIdVal("datatype", 2),
      a =
        "16" == getElIdVal("inputtype", 2)
          ? "H" + a.value
          : "7" == g
          ? "'" + a.value + "'"
          : a.value;
    tstValue = value;
    g = "";
    //检查语言设定并弹出对应确认消息
    0 == checkFileLanguage()
      ? (g = c + "[ " + b + " ]" + d + "[ " + a + " ]" + e + "\n" + f)
      : 1 == checkFileLanguage()
      ? (g = "[ " + a + " ]" + d + "[ " + b + " ]" + e + "\n" + f)
      : 2 == checkFileLanguage() &&
        (g = c + "[ " + a + " ]" + d + "[ " + b + " ]" + e + "\n" + f);
    setInTxt("confirmmsg", g, 2);
    showMessage(2);
  }
}
//can select button
function onCanselBtn() {
  window.modalCloseDevTest();
  showMessage(1);
  showTest = 0;
}
//消息显示
function showMessage(a) {
  var b = getElIdObj("writesetting", 2),
    c = getElIdObj("confirmarea", 2),
    d = getElIdObj("messagearea", 2),
    e = getElIdObj("infomessagearea", 2);
  e.style.display = "none";
  switch (a) {
    case 1:
      b.style.display = "";
      c.style.display = "none";
      d.style.display = "none";
      break;
    case 2:
      b.style.display = "none";
      c.style.display = "";
      d.style.display = "none";
      getElIdObj("yesbtn", 2).focus();
      break;
    case 3:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "";
      getElIdObj("okbtn", 2).focus();
      break;
    case 40:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "none";
      e.style.display = "";
      getElIdObj("infookbtn", 2).style.display = "";
      getElIdObj("infookbtn", 2).focus();
      break;
    case 41:
      (b.style.display = "none"),
        (c.style.display = "none"),
        (d.style.display = "none"),
        (e.style.display = ""),
        (getElIdObj("infookbtn", 2).style.display = "none");
  }
}
//确认按钮
function confirmOKBtn() {
  if (0 < writing)
    setInTxt("infomessage", alertMessage("devicetest_now", 1), 2),
      showMessage(41);
  else {
    var a = new ArrayBuffer(16),
      b = new DataView(a),
      c = getElIdObj("datatype", 2),
      a = getInTxt("device", 2),
      d;
    d = checkDevtype(a);
    "S" == d && (d = "W");
    "W" !== d ||
      ("4" !== c.value && "5" !== c.value && "6" !== c.value) ||
      (d = "D");
    "DS" == d && (d = "D");
    //testValue 不是 undefined就写入（writing = 1写入操作中）
    void 0 == tstVal
      ? onCanselBtn()
      : ((a = changeDevItem(a)),
        setDataView(tstValue, c.value, b, !0),
        (b = getDataView16(c.value, b, !0)),
        (writing = 1),
        WriteDeviceTest(a, d, b));
  }
}
//取消
function confirmNOBtn() {
  showMessage(1);
}
//写入成功消息确认
function messageOKBtn(a) {
  "info" === a && (writing = 0);
  onCanselBtn();
}
//检查设备对应数据类型
function checkDevtype(a, b) {
  var c = "ERR",
    d = [
      ["LCN", 3, "D"],
      ["LCS", 3, "B"],
      ["LCC", 3, "B"],
      ["LC", 2, "D"],
      ["STN", 3, "S"],
      ["STS", 3, "B"],
      ["STC", 3, "B"],
      ["ST", 2, "S"],
      ["CN", 2, "S"],
      ["CS", 2, "B"],
      ["CC", 2, "B"],
      ["C", 1, "S"],
      ["TN", 2, "S"],
      ["TS", 2, "B"],
      ["TC", 2, "B"],
      ["T", 1, "S"],
      ["LZ", 2, "DS"],
      ["SD", 2, "W"],
      ["SW", 2, "W"],
      ["SM", 2, "B"],
      ["SB", 2, "B"],
      ["BL", 2, "B"],
      ["Z", 1, "W"],
      ["R", 1, "W"],
      ["W", 1, "W"],
      ["D", 1, "W"],
      ["S", 1, "B"],
      ["F", 1, "B"],
      ["B", 1, "B"],
      ["L", 1, "B"],
      ["M", 1, "B"],
      ["Y", 1, "B"],
      ["X", 1, "B"],
      ["U0\\G", 4, "W"],
      ["U1\\G", 4, "W"],
      ["U2\\G", 4, "W"],
      ["U3\\G", 4, "W"],
      ["U4\\G", 4, "W"],
      ["U5\\G", 4, "W"],
      ["U6\\G", 4, "W"],
      ["U7\\G", 4, "W"],
      ["U8\\G", 4, "W"],
      ["U9\\G", 4, "W"],
      ["UA\\G", 4, "W"],
      ["UB\\G", 4, "W"],
      ["UC\\G", 4, "W"],
      ["UD\\G", 4, "W"],
      ["UE\\G", 4, "W"],
      ["UF\\G", 4, "W"],
      ["U10\\G", 5, "W"],
    ],
    e,
    f,
    g;
    //测试是否是PLC模块
  f = new RegExp(/BL[0-9]+\\S[0-9]+$/);
  if (f.test(a))
    return (
      b &&
        ((g = a.match(/\d+$/)),
        (g = parseInt(g, 10).toString(10)),
        (c = a.match(/^.*S/)),
        (b[0] = c + g)),
      "B"
    );
    //16进制0-F还是8进制0-7还是10进制
  for (var h = 0; h < d.length; h++)
    if (d[h][0] == a.substr(0, d[h][1])) {
      e = a.slice(d[h][1]);
      if ("" == e) break;
      f = getNumType(d[h][0]);
      16 == f
        ? ((f = new RegExp(/^[0-9a-fA-F]+$/)),
          b && (g = parseInt(e, 16).toString(16).toLocaleUpperCase()))
        : ((f = 8 == f ? new RegExp(/^[0-7]+$/) : new RegExp(/^[0-9]+$/)),
          b && (g = parseInt(e, 10).toString(10)));
      f.test(e) && ((c = d[h][2]), b && (b[0] = d[h][0] + g));
      break;
    }
  return c;
}
//批量监视换页
function changePageBat(a) {
  if (!(0 < nextClk)) {
    nextClk++;
    setElDisable("nextbutton", !0, 1);
    var b = getTargetIDHead(nowDisplay)[0];
    4 == nowDisplay
      ? (b = "LZ" == mainDevType ? b * BIT_POINT_4 : b * BIT_POINT_8)
      : 18 == nowDisplay && (b *= BIT_POINT_8);
    0 < a ? (startPoint += b) : 0 > a && (startPoint -= b);
    startPoint = parseInt(startPoint);
    0 >= startPoint && (startPoint = 0);
    monBatStartStop(isBatMonitor);
    getElIdObj("wordtable1", 1).scrollTop = 0;
    getElIdObj("bit8table1", 1).scrollTop = 0;
    getElIdObj("bit10table1", 1).scrollTop = 0;
    getElIdObj("c_t_table1", 1).scrollTop = 0;
  }
}
//设备测试弹窗
function onDevTestLayer(a, b) {
  if (0 != isBatMonitor) {
    var c,
      d = getElIdObj("formattype", 1),
      e = getElIdObj("datatype", 1);
    if (0 == mainSetType)
      switch (nowDisplay) {
        case 3:
          c = getC_T_DeviceStr(a, b);
          3 == b
            ? ((d = getElIdObj("formattype", 1)),
              (e = getElIdObj("datatype", 1)))
            : ((d = document.createElement("select")),
              (e = document.createElement("select")),
              setRegMonSelect(d, e, "B"));
          break;
        case 8:
        case 10:
        case 4:
        case 18:
          c = getTargetIDHead(nowDisplay);
          c = getDeviceStr(a, b, c[2], mainBitsortSel);
          break;
        default:
          c = getInTxt("dev" + a, 1);
      }
    else if (1 == mainSetType)
      switch (nowDisplay) {
        case 4:
        case 18:
          c = getTargetIDHead(nowDisplay);
          c = getDeviceStr(a, b, c[2], mainBitsortSel);
          break;
        default:
          c = getInTxt("dev" + a, 1);
      }
    "" != c &&
      ((tstDev = c),
      (tstFmt = d),
      (tstType = e),
      (tstVal = getTargetValueObject(a, b)),
      (tstUpdateFn = function () {
        monBatStartStop(isBatMonitor);
      }),
      modalOpenDevTest());
  }
}
//注册监视页面设备测试弹窗
function onRegMonDevTestLayer(a) {
  if (0 != isRegMonitor) {
    checkWacthDev(0);
    var b = getElIdVal("dev" + a, 1);
    0 != (WATCH_AOK & mainDevRegData[a - 1][3]) &&
      ((tstDev = b),
      (tstFmt = getElIdObj("devfmt" + a, 1)),
      (tstType = getElIdObj("devsize" + a, 1)),
      (tstVal = getElIdObj("val" + a, 1)),
      (tstUpdateFn = function () {
        monRegStartStop(isRegMonitor);
      }),
      modalOpenDevTest());
  }
}
//更换watch（监视）表1或2
function changePageReg(a) {
  var b = getElIdObj("wordtable1", 1),
    c = getElIdObj("wordtable2", 1),
    d = getElIdObj("watchbtn1", 1),
    e = getElIdObj("watchbtn2", 1);
  switch (a) {
    case 1:
      b.style.display = "";
      c.style.display = "none";
      d.style.color = WATCH_BTN_SELECT;
      d.style.backgroundColor = WATCH_BTN_SELECT_BG;
      d.style.fontWeight = "bold";
      e.style.color = "";
      e.style.backgroundColor = "";
      e.style.fontWeight = "";
      viewPageNum = 1;
      break;
    case 2:
      (b.style.display = "none"),
        (c.style.display = ""),
        (d.style.className = "watchbtn"),
        (e.style.className = "selwatchbtn"),
        (d.style.color = ""),
        (d.style.backgroundColor = ""),
        (d.style.fontWeight = ""),
        (e.style.color = WATCH_BTN_SELECT),
        (e.style.backgroundColor = WATCH_BTN_SELECT_BG),
        (e.style.fontWeight = "bold"),
        (viewPageNum = 2);
  }
  setStorage("viewPageNum", viewPageNum);
}
//更换显示页面
function changeDisplay(a) {
  var b = getElIdObj("monitortable1", 1),
    c = getElIdObj("monitortable2", 1),
    d = getElIdObj("monitortable3", 1),
    e = getElIdObj("monitortable4", 1),
    f = getElIdObj("monitortable5", 1),
    g = getElIdObj("monitortable6", 1);
  switch (a) {
    case 3:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "none";
      e.style.display = "";
      f.style.display = "none";
      g.style.display = "none";
      setElDisable("partdisptype", !1, 1);
      nowDisplay = 3;
      break;
    case 4:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "none";
      e.style.display = "none";
      f.style.display = "none";
      g.style.display = "";
      setElDisable("partdisptype", !1, 1);
      nowDisplay = 4;
      break;
    case 8:
      b.style.display = "none";
      c.style.display = "";
      d.style.display = "none";
      e.style.display = "none";
      f.style.display = "none";
      g.style.display = "none";
      setElDisable("partdisptype", !0, 1);
      nowDisplay = 8;
      break;
    case 10:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "";
      e.style.display = "none";
      f.style.display = "none";
      g.style.display = "none";
      setElDisable("partdisptype", !0, 1);
      nowDisplay = 10;
      break;
    case 18:
      b.style.display = "none";
      c.style.display = "none";
      d.style.display = "none";
      e.style.display = "none";
      f.style.display = "";
      g.style.display = "none";
      setElDisable("partdisptype", !1, 1);
      nowDisplay = 18;
      break;
    default:
      (b.style.display = ""),
        (c.style.display = "none"),
        (d.style.display = "none"),
        (e.style.display = "none"),
        (f.style.display = "none"),
        (g.style.display = "none"),
        setElDisable("partdisptype", !1, 1),
        (nowDisplay = 1);
  }
}
//获取位设备字符串
function getBitDeviceStr(a, b, c) {
  var d = mainDevType,
    e = [],
    f = getNumType(d),
    g = getElIdVal("devaddress", 1).value,
    h = convertDecNum(d, g);
  startPoint != h && (g = startPoint.toString(f));
  h = "";
  if ("" == getElIdObj("bit" + nowDisplay + "val" + a + "_" + b, 1).value)
    return h;
  for (var k = (h = 0); k < MAX_DISP_LINE * nowDisplay; k += nowDisplay) {
    e[h] = [];
    for (var m = 0; m < nowDisplay; m++) {
      var l = g;
      16 == f
        ? ((l = "0x" + g), (l = parseInt(l, 16)))
        : 8 == f && ((l = "0" + g), (l = parseInt(l, 8)));
      l = parseInt(l, 10);
      l = (parseInt(k + m, 10) + l).toString(f).toUpperCase();
      0 == l.search(/[A-F]/) && (l = "0" + l);
      e[h][m] = d + l;
    }
    h += 1;
  }
  c && e[a - 1].reverse();
  return (h = e[a - 1][b - 1]);
}
//改变设备X输入 / Y输出类型
function changeDevItem(a) {
  var b = a;
  0 == b.indexOf("X")
    ? ((b = b.substr(b.indexOf("X") + 1, b.length)),
      (b = parseInt(b, 8)),
      (a = "X" + b.toString(16)))
    : 0 == b.indexOf("Y") &&
      ((b = b.substr(b.indexOf("Y") + 1, b.length)),
      (b = parseInt(b, 8)),
      (a = "Y" + b.toString(16)));
  return a;
}
function onTimerStartForW10() {
  onTimerStopForW10();
  timeout_10 <= 1e3 * mainTimerCycle &&
    (timerID_10 = setInterval(ReqCgiForW10, 0.5 * timeout_10));
}
function onTimerStopForW10() {
  timerID_10 && (clearInterval(timerID_10), (timerID_10 = 0));
}
//发空消息测试连接状态？ 
function ReqCgiForW10() {
  try {
    var a;
    onTimerStopForW10();
    a = new XMLHttpRequest();
    a.open("POST", "/cgi/RdDevBlk.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      ReqCgiForW10_Response(a);
    };
    a.send(null);
  } catch (b) {
    console.log(b.message);
  }
}
function ReqCgiForW10_Response(a) {}

//检查输入地址是否存在
function checkInputDevice(a, b, c) {
  var d = 1,
    e;
  onTimerStopForW10();
  "W" == c && (d = 2);
  e = changeDevItem(a);
  param =
    "NUM=" +
    encodeURIComponent(d.toString(16)) +
    "&DEV=" +
    encodeURIComponent(e);
  try {
    var f;
    f = new XMLHttpRequest();
    f.open("POST", "/cgi/RdDevBlk.cgi", !0);
    f.setRequestHeader(HDTYP, HDVAL);
    f.onreadystatechange = function () {
      checkInputDevice_Response(f, a, b, c);
    };
    f.send(param);
  } catch (g) {
    setErrorItemReg(b), console.log(g.message);
  }
}
function checkInputDevice_Response(a, b, c, d) {
  if (4 != a.readyState || 200 != a.status)
    3 == a.readyState && onTimerStartForW10();
  else {
    a = JSON.parse(a.response);
    if ("0000" === a.RET) {
      a = a.DATA;
      var e = getElIdObj("dev" + c, 1),
        f = getElIdObj("devfmt" + c, 1),
        g = getElIdObj("devsize" + c, 1);
      "W" == d && 1 == a.length
        ? ((d = "WS"), (mainDevRegData[c - 1][3] = WATCH_OKE))
        : (mainDevRegData[c - 1][3] = WATCH_OK);
      setRegMonSelect(f, g, d);
      e.style.backgroundColor = "";
      e.value = b;
      saveWatchTable(c);
      monRegStartStop(1);
      (b = getElIdObj("dev" + (c + 1), 1)) && b.focus();
    } else
      "0001" === a.RET ? alertMessage(a.RET, 0) : alertMessage(a.RET, 2),
        setErrorItemReg(c);
    entChk = 0;
  }
}
//设置空值样式
function setEmptyItemReg(a) {
  var b = getElIdObj("dev" + a, 1),
    c = getElIdObj("devfmt" + a, 1),
    d = getElIdObj("devsize" + a, 1),
    e = getElIdObj("val" + a, 1);
  mainDevRegData[a - 1][3] = "";
  c.options.length = 0;
  d.options.length = 0;
  b.style.backgroundColor = "";
  b.value = "";
  e.value = "";
  setLineColorMon(a, "", "");
  saveWatchTable(a);
}
//设置错误样式
function setErrorItemReg(a) {
  var b = getElIdObj("dev" + a, 1),
    c = getElIdObj("devfmt" + a, 1),
    d = getElIdObj("devsize" + a, 1),
    e = getElIdObj("val" + a, 1);
  mainDevRegData[a - 1][3] = WATCH_ERR;
  b.style.backgroundColor = "rgb(255,192,192)";
  c.options.length = 0;
  d.options.length = 0;
  e.value = "";
  saveWatchTable(a);
}
//设置显示位值的排序方式
function setBitSortPull(a, b) {
  a.options.length = 0;
  switch (b) {
    case 8:
    case 18:
      a.options[0] = new Option("0-7");
      a.options[0].value = 0;
      a.options[1] = new Option("7-0");
      a.options[1].value = 1;
      a.options[1].selected = !0;
      setElDisable(a.id, !1, 1);
      break;
    case 4:
      a.options[0] = new Option("0-3");
      a.options[0].value = 0;
      a.options[1] = new Option("3-0");
      a.options[1].value = 1;
      a.options[1].selected = !0;
      setElDisable(a.id, !1, 1);
      break;
    case 10:
      a.options[0] = new Option("0-9");
      a.options[0].value = 0;
      a.options[1] = new Option("9-0");
      a.options[1].value = 1;
      a.options[1].selected = !0;
      setElDisable(a.id, !1, 1);
      break;
    default:
      (a.options[0] = new Option("--")),
        (a.options[0].value = 0),
        (a.options[0].selected = !0),
        setElDisable(a.id, !0, 1),
        (mainBitsortSel = "0");
  }
}
function onchangeBitSortPull() {
  makeBitDisplayHead(getDisplayType(mainDevType));
  monBatStartStop(isBatMonitor);
  mainBitsortSel = getElIdVal("bitsortpull", 1);
  setStorage("mainBitsortSel", mainBitsortSel);
}
function makeBitDisplayHead(a) {
  var b = getElIdObj("bitsortpull", 1);
  if (8 == a || 10 == a)
    if (0 == b.value)
      for (b = 0; b < a; b++)
        getElIdObj("bithead" + a + "_" + b, 1).innerText = a - (b + 1);
    else
      for (b = a; b--; ) getElIdObj("bithead" + a + "_" + b, 1).innerText = b;
  else if (18 == a)
    if (0 == b.value)
      for (a = 0; 8 > a; a++)
        getElIdObj("w_multihead8_" + a, 1).innerText = "+" + (8 - (a + 1));
    else
      for (a = 8; a--; ) getElIdObj("w_multihead8_" + a, 1).innerText = "+" + a;
  else if (4 == a)
    if ("D" == getDevType(0 == mainSetType ? mainDevType : "D"))
      if (0 == b.value)
        for (a = 0; 4 > a; a++)
          getElIdObj("dw_multihead4_" + a, 1).innerText = "+" + (3 - a);
      else
        for (a = 4; a--; )
          getElIdObj("dw_multihead4_" + a, 1).innerText = "+" + a;
    else if (((a = 0), 0 == b.value))
      for (b = 0; 4 > b; b++)
        (getElIdObj("dw_multihead4_" + b, 1).innerText = "+" + (6 - (b + a))),
          a++;
    else
      for (b = 4; b--; )
        (getElIdObj("dw_multihead4_" + b, 1).innerText = "+" + (6 - a)),
          (a += 2);
}
//复制选中的项
function cpySelectItem(a, b) {
  for (var c = (b.options.length = 0); c < a.options.length; c++)
    (b.options[c] = new Option(a.options[c].label)),
      b.options[c] && (b.options[c].value = a.options[c].value);
  b.options.selectedIndex = a.options.selectedIndex;
}
//输入批量读取检查确认
function checkInputDeviceBat(a, b) {
  var c, d;
  onTimerStopForW10();
  c = changeDevItem(a);
  param =
    "NUM=" +
    encodeURIComponent((1).toString(16)) +
    "&DEV=" +
    encodeURIComponent(c);
  try {
    var e;
    e = new XMLHttpRequest();
    e.open("POST", "/cgi/RdDevBlk.cgi", !0);
    e.setRequestHeader(HDTYP, HDVAL);
    e.onreadystatechange = function () {
      checkInputDeviceBat_Response(e, a);
    };
    e.send(param);
  } catch (f) {
    1 == getElIdObj("radio001", 1).checked
      ? ((d = getElIdObj("devaddress", 1)),
        setErrorItemBat(d),
        (d = getElIdObj("bl_address", 1)))
      : 1 == getElIdObj("radio002", 1).checked &&
        (d = getElIdObj("memaddress", 1)),
      setErrorItemBat(d),
      console.log(f.message);
  }
}
function checkInputDeviceBat_Response(a, b) {
  if (4 != a.readyState || 200 != a.status)
    3 == a.readyState && onTimerStartForW10();
  else {
    var c = JSON.parse(a.response),
      d;
    if ("0000" === c.RET) {
      var e,
        c = mainDevType,
        f = mainSetType,
        g = mainBlSelect;
      getElIdObj("radio001", 1).checked
        ? ((mainSetType = 0),
          (d = getElIdObj("devaddress", 1)),
          (mainDevType = getElIdVal("devselect", 1)),
          setStorage("mainDevType", mainDevType),
          (mainDevAddress = getElIdVal("devaddress", 1)),
          setStorage("mainDevAddress", mainDevAddress),
          (mainBlSelect = getElIdVal("bl_select", 1)),
          "BL" == mainDevType &&
            ((mainBlSelect = getElIdVal("bl_select", 1)),
            setStorage("mainBlSelect", mainBlSelect),
            (mainBlAddress = getElIdVal("bl_address", 1)),
            setStorage("mainBlAddress", mainBlAddress)),
          (e = convertDecNum(mainDevType, mainDevAddress)),
          "BL" == mainDevType &&
            "S" == mainBlSelect &&
            (e = convertDecNum(mainDevType, mainBlAddress)),
          (getElIdObj("bl_address", 1).style.backgroundColor = ""))
        : getElIdObj("radio002", 1).checked &&
          ((mainSetType = 1),
          (mainBfmUnitNo = getElIdVal("moduleNo", 1)),
          setStorage("mainBfmUnitNo", mainBfmUnitNo),
          (d = getElIdObj("memaddress", 1)),
          (mainBfmAddress = getElIdVal("memaddress", 1)),
          setStorage("mainBfmAddress", mainBfmAddress),
          (e = parseInt(mainBfmAddress)));
      setStorage("mainSetType", mainSetType);
      d.style.backgroundColor = "";
      d.value = d.value.toUpperCase();
      onloadFirst && (startPoint = e);
      onloadFirst = !1;
      0 >= startPoint && (startPoint = 0);
      if (c != mainDevType || f != mainSetType || g != mainBlSelect)
        changeMonitorSelectBox(),
          (d = 0 == mainSetType ? mainDevType : "D"),
          setMultiSelect(getElIdObj("partdisptype", 1), checkDevtype(d + "0")),
          (dispType = getDisplayType(d)),
          setBitSortPull(getElIdObj("bitsortpull", 1), dispType),
          makeBitDisplayHead(dispType),
          changeDisplay(dispType),
          (mainDataType = getElIdVal("datatype", 1)),
          setStorage("mainDataType", mainDataType),
          changeFmtSelectStyle(
            getElIdObj("datatype", 1),
            getElIdObj("formattype", 1)
          ),
          (mainInputType = getElIdVal("formattype", 1)),
          setStorage("mainInputType", mainInputType),
          (mainMultiPoint = getElIdVal("partdisptype", 1)),
          setStorage("mainMultiPoint", mainMultiPoint),
          (mainBitsortSel = getElIdVal("bitsortpull", 1)),
          setStorage("mainBitsortSel", mainBitsortSel);
      monBatStartStop(1);
    } else
      1 == getElIdObj("radio001", 1).checked
        ? ((d = getElIdObj("devaddress", 1)),
          setErrorItemBat(d),
          (d = getElIdObj("bl_address", 1)))
        : 1 == getElIdObj("radio002", 1).checked &&
          (d = getElIdObj("memaddress", 1)),
        "0001" === c.RET ? alertMessage(c.RET, 0) : alertMessage(c.RET, 2),
        setErrorItemBat(d);
  }
}
//处理响应后显示
function setErrorItemBat(a) {
  a.style.backgroundColor = "rgb(255,192,192)";
}
function putReadDataBat(a, b) {
  var c = 0,
    d = MAX_DISP_LINE,
    e,
    f = !1,
    g = getDevType(0 == mainSetType ? mainDevType : "D");
  setDevBatMon(b);
  if ("B" == g) {
    var g = [],
      h = 0;
    e = makeBitArray(a, b);
    for (var k = 0; k < e.length; k += nowDisplay) {
      g[h] = [];
      for (c = 0; c < nowDisplay; c++)
        (g[h][c] = e[k + c]), void 0 == g[h][c] && (g[h][c] = "");
      1 == mainBitsortSel && g[h].reverse();
      h += 1;
    }
    for (k = 0; k < g.length; k++)
      for (c = 0; c < nowDisplay; c++)
        (e = getElIdObj(
          "bit" + nowDisplay + "val" + (k + 1) + "_" + (c + 1),
          1
        )),
          (e.value = g[k][c]),
          setColBitBat(e, 1);
    for (k = h + 1; k <= d; k++) {
      for (f = 1; f <= nowDisplay; f++)
        (e = getElIdObj("bit" + nowDisplay + "val" + k + "_" + f, 1)),
          setColBitBat(e, 2);
      setInTxt("bit" + nowDisplay + "dev" + k, "", 1);
      f = !0;
    }
  } else {
    var h = getElIdVal("datatype", 1),
      m;
    switch (nowDisplay) {
      case 4:
        setMultiWordDouble(a, nowDisplay, h);
        break;
      case 18:
        setMultiWord(a, nowDisplay - 10, h);
        break;
      default:
        for (k = 0; k < a.length; k++)
          if ((e = getElIdObj("val" + (k + 1), !0)))
            if (((m = a[k]), "W" == g)) {
              var l;
              l = a.length >= k + 1 ? k + 1 : k;
              switch (h) {
                case "2":
                case "3":
                  e.value = getFixValue(m, h);
                  break;
                case "4":
                case "5":
                  0 == c
                    ? ((c = makeDoubleStr(a[l], m)),
                      (e.value = getFixValue(c, h)),
                      (c = 1))
                    : ((e.value = "--"), (c = 0));
                  break;
                case "6":
                  0 == c
                    ? ((c = makeDoubleStr(a[l], m)),
                      (e.value = changeFloatNum(c)),
                      (c = 1))
                    : ((e.value = "--"), (c = 0));
                  break;
                case "7":
                  e.value = changeAsciiStr(m);
              }
            } else
              "D" == g &&
                (e.value = "6" == h ? changeFloatNum(m) : getFixValue(m, h));
        for (e = k + 1; e <= d; e++)
          setElIdVal("val" + e, "", 1), setInTxt("dev" + e, "", 1), (f = !0);
    }
  }
  setElDisable("nextbutton", f, 1);
}

//读取设备块
function ReadDeviceBlock_Continue(a, b) {
  var c = MAX_DISP_LINE,
    d = mainDevType,
    e = mainDevAddress,
    f,
    g = convertDecNum(d, e);
  startPoint != g && (e = parseInt(startPoint));
  parseInt(e);
  var h;
  switch (b) {
    case 0:
      h = "S";
      break;
    case 1:
      h = "C";
      break;
    case 2:
      h = "N";
  }
  3 == getDisplayType(d) && (f = d + h + parseInt(e));
  c =
    "NUM=" +
    encodeURIComponent(c.toString(16)) +
    "&DEV=" +
    encodeURIComponent(f);
  try {
    (a[b] = new XMLHttpRequest()),
      a[b].open("POST", "/cgi/RdDevBlk.cgi", !0),
      a[b].setRequestHeader(HDTYP, HDVAL),
      (a[b].onreadystatechange = function () {
        ReadDeviceBlock_Continue_Response(a, b);
      }),
      a[b].send(c);
  } catch (k) {
    console.log(k.message);
  }
}


function ReadDeviceBlock_Continue_Response(a, b) {
  if (4 != a[b].readyState || 200 != a[b].status)
    3 == a[b].readyState && onTimerStartForW10();
  else if ("" != a[b].response) {
    var c = JSON.parse(a[b].response),
      d = [];
    if ("0000" === c.RET) {
      d = c.DATA;
      if (2 > b) var e = makeBitArray(d, parseInt(c.NUM, 16));
      switch (b) {
        case 0:
          c_t_DataArray[0] = [];
          for (d = 0; d < e.length; d++) c_t_DataArray[0][d] = e[d];
          ReadDeviceBlock_Continue(a, 1);
          break;
        case 1:
          c_t_DataArray[1] = [];
          for (d = 0; d < e.length; d++) c_t_DataArray[1][d] = e[d];
          ReadDeviceBlock_Continue(a, 2);
          break;
        case 2:
          c_t_DataArray[2] = [];
          for (e = 0; e < d.length; e++) c_t_DataArray[2][e] = d[e];
          putReadDataBat_Continue(c_t_DataArray);
          viewarea.document.getElementById("loading-win").style.display =
            "none";
      }
    } else
      "0001" === c.RET
        ? monBatStartStop(0)
        : ("4031" === c.RET &&
            (1 == nextClk
              ? ((nextClk = 0),
                (d[0] = []),
                (d[1] = []),
                (d[2] = []),
                putReadDataBat_Continue(d),
                changePageBat(-1),
                setElDisable("nextbutton", !0, 1))
              : monBatStartStop(0)),
          alertMessage(c.RET, 2)),
        (viewarea.document.getElementById("loading-win").style.display =
          "none");
    2 == b && (nextClk = 0);
  }
}


function putReadDataBat_Continue(a) {
  var b = MAX_DISP_LINE,
    c = !1;
  setDevBatMon(a[0].length);
  for (var d = 0; d < a[0].length; d++) {
    var e = d + 1;
    setElIdVal("val_s" + e, a[0][d], 1);
    setColBitCTBat("val_s" + e, a[0][d]);
  }
  for (d = 0; d < a[1].length; d++)
    (e = d + 1),
      setElIdVal("val_c" + e, a[1][d], 1),
      setColBitCTBat("val_c" + e, a[1][d]);
  for (d = 0; d < a[2].length; d++)
    (e = d + 1),
      setElIdVal(
        "val_n" + e,
        getFixValue(a[2][d], getElIdVal("datatype", 1)),
        1
      );
  for (a = d + 1; a <= b; a++)
    setElIdVal("val_s" + a, "", 1),
      setElIdVal("val_c" + a, "", 1),
      setElIdVal("val_n" + a, "", 1),
      setInTxt("c_t_dev" + a, "", 1),
      (c = !0);
  setElDisable("nextbutton", c, 1);
}


function getC_T_DeviceStr(a, b) {
  var c = [],
    d,
    e = "";
  c[0] = [];
  c[1] = [];
  c[2] = [];
  if ("" == getInTxt("c_t_dev" + a, 1)) return retValue;
  for (var f = 0; f < c_t_DataArray.length; f++)
    for (var g = 0; g < c_t_DataArray[f].length; g++) {
      d = parseInt(g) + 0 + parseInt(startPoint);
      switch (f) {
        case 0:
          e = mainDevType + "S" + d;
          break;
        case 1:
          e = mainDevType + "C" + d;
          break;
        case 2:
          e = mainDevType + "N" + d;
      }
      c[f][g] = e;
    }
  return c[b - 1][a - 0 - 1];
}


function getTargetValueObject(a, b) {
  var c,
    d = mainDevType;
  1 == mainSetType && (d = "D");
  switch (getDisplayType(d)) {
    case 3:
      switch (b) {
        case 1:
          c = getElIdObj("val_s" + a, 1);
          break;
        case 2:
          c = getElIdObj("val_c" + a, 1);
          break;
        case 3:
          c = getElIdObj("val_n" + a, 1);
      }
      break;
    case 8:
      c = getElIdObj("bit8val" + a + "_" + b, 1);
      break;
    case 10:
      c = getElIdObj("bit10val" + a + "_" + b, 1);
      break;
    case 4:
      c = getElIdObj("dw_multi4val" + a + "_" + b, 1);
      break;
    case 18:
      c = getElIdObj("w_multi8val" + a + "_" + b, 1);
      break;
    default:
      c = getElIdObj("val" + a, 1);
  }
  return c;
}

function onchangeTstType() {
  var a = getElIdObj("datatype", 2),
    b = getElIdObj("inputtype", 2),
    c = getElIdObj("setvalue", 2);
  changeFmtSelectStyle(a, b);
  changeValueLength(a, b, c);
  c.value = "";
}

function onchangeTstFmt() {
  var a,
    b = getElIdObj("datatype", 2),
    c = getElIdObj("inputtype", 2),
    d = getElIdObj("setvalue", 2);
  a = 16 == c.value ? parseInt(d.value, 10) : parseInt(d.value, 16);
  changeValueLength(b, c, d);
  if (isNaN(a)) d.value = "";
  else {
    var e = new ArrayBuffer(16),
      e = new DataView(e);
    setDataView(a, b.value, e, !0);
    d.value =
      16 == c.value
        ? getDataView16(b.value, e, !0).toUpperCase()
        : getDataView10(b.value, e, !0);
  }
}

//改变值的长度
function changeValueLength(a, b, c) {
  switch (a.value) {
    case "1":
      c.maxLength = 1;
      break;
    case "3":
      c.maxLength = 16 == b.value ? 4 : 5;
      break;
    case "4":
      c.maxLength = 16 == b.value ? 8 : 11;
      break;
    case "5":
      c.maxLength = 16 == b.value ? 8 : 10;
      break;
    case "6":
      c.maxLength = 14;
      break;
    case "7":
      c.maxLength = 2;
      break;
    default:
      c.maxLength = 16 == b.value ? 4 : 6;
  }
}


function changeFmtSelectStyle(a, b) {
  switch (a.value) {
    case "1":
      break;
    case "2":
    case "3":
    case "4":
    case "5":
      b.options[0].text = getRadixTypeStr(10);
      b.disabled = !1;
      break;
    case "6":
    case "7":
      b.options[0].text = "--";
      b.value = 10;
      b.disabled = !0;
      break;
    default:
      (b.value = 10), (b.disabled = !1);
  }
}
//转换输入的值
function convertInputVal() {
  var a = getElIdObj("datatype", 2),
    b = getElIdObj("inputtype", 2),
    c = getElIdObj("setvalue", 2),
    d,
    e = "";
  if ("" == c.value) return e;
  switch (b.value) {
    case "2":
      d = new RegExp(/^[0-1]+$/);
      break;
    case "16":
      d = new RegExp(/^[-+0-9A-Fa-f]+$/);
      break;
    default:
      d =
        "2" == a.value || "4" == a.value
          ? new RegExp(/^[-+0-9]+$/)
          : "3" == a.value || "5" == a.value
          ? new RegExp(/^[+0-9]+$/)
          : "6" == a.value
          ? new RegExp(/^[-+.eE0-9]+$/)
          : new RegExp(/^[0-9]+$/);
  }
  switch (a.value) {
    case "1":
      d.test(c.value) && (e = parseInt(c.value, 2));
      break;
    case "2":
    case "4":
      d.test(c.value) &&
        ("10" == b.value
          ? checkSigned(c.value, 1) &&
            ((e = parseInt(c.value, 10)), (e = rangeCheckInt(e, a.value)))
          : checkSigned(c.value, 2) &&
            ((c.value = croppedstr), (e = parseInt(c.value, 16))));
      break;
    case "3":
    case "5":
      d.test(c.value) &&
        ("10" == b.value
          ? checkSigned(c.value, 1) &&
            ((e = parseInt(c.value, 10)), (e = rangeCheckInt(e, a.value)))
          : checkSigned(c.value, 0) && (e = parseInt(c.value, 16)));
      break;
    case "6":
      d.test(c.value) &&
        checkFloat(c.value) &&
        ((e = parseFloat(c.value)), (e = rangeCheckFloat(e)));
      break;
    case "7":
      return (e = rangeCheckString(c.value));
  }
  return isNaN(e) ? "" : e;
}
//写入测试
function WriteDeviceTest(a, b, c) {
  var d = "";
  onTimerStopForW10();
  d = "NUM=1&" + ("DEV1=" + a + "&") + ("TYP1=" + b + "&") + ("DATA1=" + c);
  try {
    (writeXhr = new XMLHttpRequest()),
      writeXhr.open("POST", "/cgi/WrDev.cgi", !0),
      writeXhr.setRequestHeader(HDTYP, HDVAL),
      (writeXhr.timeout = xhrTimeout_msec),
      (writeXhr.ontimeout = function (a) {
        console.error("xhr.ontimeout");
        writing = 0;
        setInTxt("infomessage", alertMessage("devicetest_error", 1), 2);
        showMessage(40);
      }),
      (writeXhr.onerror = function (a) {
        console.error("xhr.onerror : [ " + a.type + " ] ");
        writing = 0;
        setTimeout(function () {
          setInTxt("infomessage", alertMessage("devicetest_error", 1), 2);
          showMessage(40);
        }, xhrTimeout_msec);
      }),
      (writeXhr.onreadystatechange = function () {
        WriteDeviceTest_Response();
      }),
      writeXhr.send(d);
  } catch (e) {
    console.log(e.message);
  }
}

function WriteDeviceTest_Response() {
  if (4 != writeXhr.readyState || 200 != writeXhr.status)
    3 == writeXhr.readyState && onTimerStartForW10();
  else {
    try {
      var a = JSON.parse(writeXhr.response);
      if ("0000" === a.RET) tstUpdateFn && tstUpdateFn(), onCanselBtn();
      else {
        var b;
        "0001" === a.RET
          ? alertMessage(a.RET, 0)
          : ((b = alertMessage(a.RET, 1)),
            setInTxt("message", b, 2),
            showMessage(3));
      }
    } catch (c) {
      console.log(c.message);
    }
    writing = 0;
  }
}

function checkSigned(a, b) {
  var c = a.length,
    d = !0,
    e = 0;
  croppedstr = "";
  for (
    1 == b
      ? ("-" != a.charAt(e) && "+" != a.charAt(e)) || e++
      : 2 == b && "-" == a.charAt(e) && e++;
    e < c;
    e++
  )
    if ("-" == a.charAt(e)) {
      d = !1;
      break;
    } else if ("+" == a.charAt(e)) {
      d = !1;
      break;
    } else 2 == b && (croppedstr += a.charAt(e));
  return d;
}

function checkFloat(a) {
  var b = a.length,
    c = 0;
  a = a.toUpperCase();
  var d = 0,
    e = 0,
    f = 0,
    g = 0;
  "-" == a.charAt(c) ? c++ : "+" == a.charAt(c) && c++;
  if (-1 == a.indexOf("E", c))
    for (; c < b; c++)
      switch (a.charAt(c)) {
        case "-":
          return !1;
        case "+":
          return !1;
        case ".":
          if ((d++, 1 < d)) return !1;
      }
  else {
    for (; c < b; c++)
      switch (a.charAt(c)) {
        case "-":
          f++;
          if (1 < f || "E" != a.charAt(c - 1)) return !1;
          break;
        case "+":
          g++;
          if (1 < g || "E" != a.charAt(c - 1)) return !1;
          break;
        case ".":
          d++;
          if (1 < d) return !1;
          break;
        case "E":
          if ((e++, 1 < e)) return !1;
      }
    b = a.slice(a.indexOf("E") + 1, b);
    if (0 == b.length) return !1;
    b = parseInt(b, 10);
    if (isNaN(b) || -38 > b || 38 < b) return !1;
  }
  return !0;
}

function rangeCheckInt(a, b) {
  var c = "";
  if (isNaN(a)) return c;
  switch (b) {
    case "3":
      0 <= a && 65535 >= a && (c = a);
      break;
    case "4":
      -2147483648 <= a && 2147483647 >= a && (c = a);
      break;
    case "5":
      0 <= a && 4294967295 >= a && (c = a);
      break;
    default:
      -32768 <= a && 32767 >= a && (c = a);
  }
  return c;
}

function rangeCheckFloat(a) {
  var b = "";
  if (isNaN(a)) return b;
  -3.40282347e38 <= a && 3.40282347e38 >= a && (b = a);
  return b;
}

function convertDecNum(a, b) {
  var c = getNumType(a);
  return parseInt(b, c);
}

function restrictUnderZero() {
  0 >= startPoint || isNaN(startPoint)
    ? ((startPoint = 0), setElDisable("prevbutton", !0, 1))
    : setElDisable("prevbutton", !1, 1);
}

function changeMonitorSelectBox() {
  var a = getElIdObj("formattype", 1),
    b = getElIdObj("datatype", 1);
  setRegMonSelect(
    a,
    b,
    checkDevtype((0 == mainSetType ? mainDevType : "D") + "0")
  );
}

function cancelTab(a) {
  if (0 == showTest) return !0;
  if (void 0 == a) {
    if (9 == event.keyCode) return (event.returnValue = !1);
  } else if (9 == a.which) return !1;
}

function cmnUnload() {
  1 == monStatus
    ? (monBatStartStop(0), (monStatus = 0))
    : 1 == watchStatus && (monRegStartStop(0), (watchStatus = 0));
  window.modalCloseDevTest();
  logoutBtnDisable(!0);
}

function onKeydDwnInput(a) {
  13 == a && getElIdObj("setbtn", 2).focus();
}

function fixDevAddress() {
  var a = getElIdObj("devaddress", 1),
    b = getElIdVal("devselect", 1),
    c = a.value,
    d = checkDevtype(b + c);
  if ("ERR" === d) setErrorItemBat(a), a.select();
  else {
    var e = getElIdVal("bl_select", 1),
      f = getElIdObj("bl_address", 1);
    if (
      "BL" == b &&
      "S" == e &&
      ((d = "\\S" + f.value), (c += d), (d = checkDevtype(b + c)), "ERR" === d)
    ) {
      setErrorItemBat(f);
      a.select();
      return;
    }
    checkInputDeviceBat(b + c, d);
  }
}

function fixBfmAddress() {
  var a = getElIdVal("moduleNo", 1),
    b = getElIdObj("memaddress", 1),
    a = "U" + a + "\\G" + b.value,
    c = checkDevtype(a);
  "ERR" === c ? (setErrorItemBat(b), b.select()) : checkInputDeviceBat(a, c);
}

function watchBtnClick(a) {
  changePageReg(a);
  monRegStartStop(isRegMonitor);
}

function checkWacthDev(a) {
  var b = 0,
    c,
    d,
    e,
    f = mainDevRegData;
  2 == viewPageNum && (b = MAX_DISP_LINE);
  for (var g = 0; g < MAX_DISP_LINE; g++)
    (c = g + b),
      (d = c + 1),
      a != d &&
        ((e = getElIdVal("dev" + d, 1)),
        e != f[c][0]
          ? setEmptyItemReg(d)
          : (("" != e && "" == f[c][1] && f[c][3] != WATCH_ERR) ||
              ("" == e && "" != f[c][1])) &&
            setEmptyItemReg(d));
}

function onWatchDataClear() {
  dataClearReview();
}

function dataClearReview() {
  var a = getErrorStr("msg_clear");
  ret = confirm(a);
  1 == ret &&
    (localStorageClear(), nullLiteral(), arrayClear(), reloadChildFrame());
}

function nullLiteral() {
  for (var a = 1; a <= 2 * MAX_DISP_LINE; a++) setElIdVal("dev" + a, "", 1);
}

function onchangeMultiSelect() {
  mainMultiPoint = getElIdVal("partdisptype", 1);
  setStorage("mainMultiPoint", mainMultiPoint);
  var a = getDisplayType(0 == mainSetType ? mainDevType : "D");
  setBitSortPull(getElIdObj("bitsortpull", 1), a);
  "--" != getElIdObj("bitsortpull", 1).options[0].text &&
    (mainBitsortSel = "1");
  setElIdVal("bitsortpull", mainBitsortSel, 1);
  makeBitDisplayHead(a);
  changeDisplay(a);
  monBatStartStop(isBatMonitor);
}

function setMultiSelect(a, b) {
  var c = a.options;
  c.length = 0;
  switch (b) {
    case "B":
      c[0] = new Option("--");
      c[0].value = 0;
      c.disabled = !0;
      break;
    case "S":
      c[0] = new Option(getMultiTypeStr(1));
      c[0].value = 1;
      c.disabled = !0;
      break;
    case "D":
    case "DS":
    case "W":
      "LC" == mainDevType
        ? ((c[0] = new Option(getMultiTypeStr(1))), (c[0].value = 1))
        : ((c[0] = new Option(getMultiTypeStr(1))),
          (c[0].value = 1),
          (c[1] = new Option(getMultiTypeStr(2))),
          (c[1].value = 2));
      c[0].selected = !0;
      c.disabled = !1;
      break;
    case "WS":
      (c[0] = new Option(getMultiTypeStr(1))),
        (c[0].value = 1),
        (c[0].selected = !0),
        (c.disabled = !1);
  }
}

function setMultiWordDouble(a, b, c) {
  a = "LZ" == mainDevType ? a : makeDblArray(a);
  a = makeDimArray(a, b, mainBitsortSel);
  setArrayValue(a, b, "dw_multi", c);
  return makeBlankData(a.length, b, "dw_multi", 1, MAX_DISP_LINE);
}

function setMultiWord(a, b, c) {
  a = makeDimArray(a, b, mainBitsortSel);
  setArrayValue(a, b, "w_multi", c);
  return makeBlankData(a.length, b, "w_multi", 1, MAX_DISP_LINE);
}

function makeDblArray(a) {
  for (var b = [], c = 0, d = 0; d < a.length; d++) {
    var e = makeDoubleStr(a[a.length >= d + 1 ? d + 1 : d], a[d]);
    b[c] = e;
    c++;
    d++;
  }
  return b;
}

function makeDimArray(a, b, c) {
  for (var d = [], e = 0, f = 0; f < a.length; f += b) {
    d[e] = [];
    for (var g = 0; g < b; g++)
      (d[e][g] = a[f + g]), void 0 == d[e][g] && (d[e][g] = "");
    1 == c && d[e].reverse();
    e += 1;
  }
  return d;
}

function makeBlankData(a, b, c, d, e) {
  var f = !1;
  for (a += d; a <= e; a++) {
    for (d = 1; d <= b; d++)
      if ((f = getElIdObj(c + b + "val" + a + "_" + d, 1))) f.value = "";
    setInTxt(c + b + "dev" + a, "", 1);
    f = !0;
  }
  return f;
}

function setArrayValue(a, b, c, d) {
  for (var e, f = 0; f < a.length; f++)
    for (var g = 0; g < b; g++)
      if ((e = getElIdObj(c + b + "val" + (f + 1) + "_" + (g + 1), 1)))
        e.value =
          "" == a[f][g]
            ? ""
            : "6" == d
            ? changeFloatNum(a[f][g])
            : getFixValue(a[f][g], d);
}

function getDeviceStr(a, b, c, d) {
  var e = [],
    f,
    g,
    h,
    k = 0,
    m = nowDisplay;
  0 == mainSetType
    ? ((f = mainDevType),
      (g = mainDevAddress),
      (h = getNumType(mainDevType)),
      "BL" == mainDevType && "S" == mainBlSelect && (g = mainBlAddress))
    : ((f = "U" + mainBfmUnitNo + "\\G"),
      (g = mainBfmAddress),
      (h = getNumType(f)));
  startPoint != convertDecNum(f, g) && (g = startPoint.toString(h));
  18 == m && (m -= 10);
  var l = "";
  if ("" == getElIdObj(c + m + "val" + a + "_" + b, 1).value) return l;
  c = 1;
  switch (m) {
    case 4:
    case 8:
      c = 8;
      break;
    case 10:
      c = 10;
  }
  for (l = 0; l < MAX_DISP_LINE * c; l += m) {
    e[k] = [];
    for (var p = 0; p < m; p++) {
      var n = g;
      16 == h
        ? ((n = "0x" + g), (n = parseInt(n, 16)))
        : 8 == h && ((n = "0" + g), (n = parseInt(n, 8)));
      n = parseInt(n, 10);
      n = (parseInt(l + p, 10) + n).toString(h).toUpperCase();
      0 == n.search(/[A-F]/) && (n = "0" + n);
      e[k][p] = f + n;
      0 == mainSetType &&
        "BL" == mainDevType &&
        "S" == mainBlSelect &&
        (e[k][p] = f + mainDevAddress + "\\S" + n);
      4 == m && "W" == getDevType(0 == mainSetType ? mainDevType : "D") && l++;
    }
    k += 1;
  }
  1 == d && e[a - 1].reverse();
  return (l = e[a - 1][b - 1]);
}

//bit8dev1 或者 bit10dev1 也就是PLC输入输出是10进制还是8进制
function getTargetIDHead(a) {
  var b = [];
  switch (a) {
    case 8:
      b[0] = MAX_DISP_LINE * BIT_POINT_8;
      b[1] = "bit8dev1";
      b[2] = "bit";
      break;
    case 10:
      b[0] = MAX_DISP_LINE * BIT_POINT_10;
      b[1] = "bit10dev1";
      b[2] = "bit";
      break;
    case 3:
      b[0] = MAX_DISP_LINE;
      b[1] = "c_t_dev1";
      break;
    case 4:
      b[0] = MAX_DISP_LINE;
      b[1] = "dw_multi4dev1";
      b[2] = "dw_multi";
      break;
    case 18:
      b[0] = MAX_DISP_LINE;
      b[1] = "w_multi8dev1";
      b[2] = "w_multi";
      break;
    default:
      (b[0] = MAX_DISP_LINE), (b[1] = "dev1"), (b[2] = "dev");
  }
  return b;
}

function getStartDev(a) {
  var b, c;
  0 == mainSetType
    ? ((b = mainDevType),
      (c = mainDevAddress),
      "BL" == mainDevType && "S" == mainBlSelect && (c = mainBlAddress))
    : ((b = "U" + mainBfmUnitNo + "\\G"), (c = mainBfmAddress));
  if ("" == c) return "";
  var d = getNumType(b);
  c = convertDecNum(b, c);
  a *= MAX_DISP_LINE;
  var e = c + a;
  startPoint != c && (e = startPoint + a);
  c = e.toString(d);
  0 == mainSetType &&
    "BL" == mainDevType &&
    "S" == mainBlSelect &&
    (b = b + mainDevAddress + "\\S");
  return b + c;
}

function makeBitArray(a, b) {
  for (var c = [], d = 0, e = 0; e < a.length; e++)
    for (
      var f = parseInt(a[e], 16),
        f = ("0000000000000000" + f.toString(2)).slice(-16),
        f = f.split("").reverse().join(""),
        g = 0;
      g < f.length;
      g++
    )
      (c[d] = d < b ? f[g] : void 0), d++;
  return c;
}

function setColBitBat(a, b) {
  1 == b
    ? 1 == a.value
      ? ((a.style.backgroundColor = BIT_ON_COLOR),
        (a.style.color = WATCH_BTN_SELECT))
      : ((a.style.backgroundColor = ""), (a.style.color = ""))
    : ((a.value = ""), (a.style.backgroundColor = ""), (a.style.color = ""));
}

function setColBitMon(a, b) {
  0 < b
    ? setLineColorMon(a, BIT_ON_COLOR, WATCH_BTN_SELECT)
    : setLineColorMon(a, "", "");
}

function setLineColorMon(a, b, c) {
  setBkCol("devnametd" + a, b, 1);
  setBkCol("dev" + a, b, 1);
  setBkCol("disptypetd" + a, b, 1);
  setBkCol("devfmt" + a, b, 1);
  setBkCol("datatypetd" + a, b, 1);
  setBkCol("devsize" + a, b, 1);
  setBkCol("readvaluetd" + a, b, 1);
  setBkCol("val" + a, b, 1);
  setStlCol("dev" + a, c, 1);
  setStlCol("devfmt" + a, c, 1);
  setStlCol("devsize" + a, c, 1);
  setStlCol("readvaluetd" + a, c, 1);
  setStlCol("val" + a, c, 1);
}

function setColBitCTBat(a, b) {
  var c = "",
    d = "";
  0 < b && ((c = BIT_ON_COLOR), (d = WATCH_BTN_SELECT));
  setBkCol(a, c, 1);
  setStlCol(a, d, 1);
}

function onBatMouseOver(a) {
  var b = a.value;
  if (
    "C" == mainDevType ||
    "T" == mainDevType ||
    "LC" == mainDevType ||
    "ST" == mainDevType ||
    1 == mainDataType
  )
    1 == b
      ? ((a.style.backgroundColor = LINE_HOVER_COLOR2),
        (a.style.color = LINE_HOVER_STRCOLOR))
      : ((a.style.backgroundColor = ""), (a.style.color = ""));
}

function onBatMouseOut(a) {
  var b = a.value;
  if (
    "C" == mainDevType ||
    "T" == mainDevType ||
    "LC" == mainDevType ||
    "ST" == mainDevType ||
    1 == mainDataType
  )
    1 == b
      ? ((a.style.backgroundColor = BIT_ON_COLOR),
        (a.style.color = WATCH_BTN_SELECT))
      : ((a.style.backgroundColor = ""), (a.style.color = ""));
}

function onchangeBlSelect() {
  var a = getElIdObj("bl_address", 1);
  a.focus();
  a.select();
  onloadFirst = !0;
  var b = getElIdVal("bl_select", 1),
    c = getElIdObj("bl_bkslush", 1);
  "S" == b
    ? ((a.style.visibility = "visible"), (c.style.visibility = "visible"))
    : ((a.style.visibility = "hidden"),
      (c.style.visibility = "hidden"),
      (a = getElIdObj("devaddress", 1)));
  a.focus();
  a.select();
  fixDevAddress();
}

function onBlSAddress(a) {
  if (13 == a) {
    if (
      1 == isBatMonitor &&
      mainDevType == getElIdVal("devselect", 1) &&
      mainDevAddress == getElIdVal("devaddress", 1) &&
      0 == mainSetType &&
      getElIdObj("radio001", 1).checked &&
      ((getElIdObj("devaddress", 1).style.backgroundColor = ""),
      mainBlSelect == getElIdVal("bl_select", 1) &&
        mainBlAddress == getElIdVal("bl_address", 1))
    ) {
      getElIdObj("bl_address", 1).style.backgroundColor = "";
      return;
    }
    onloadFirst = !0;
    fixDevAddress();
  }
}

//给JS运行环境打补丁
var $jscomp = { scope: {} };
$jscomp.defineProperty =
  "function" == typeof Object.defineProperties
    ? Object.defineProperty
    : function (a, c, b) {
        if (b.get || b.set)
          throw new TypeError("ES3 does not support getters and setters.");
        a != Array.prototype && a != Object.prototype && (a[c] = b.value);
      };
$jscomp.getGlobal = function (a) {
  return "undefined" != typeof window && window === a
    ? a
    : "undefined" != typeof global && null != global
    ? global
    : a;
};
$jscomp.global = $jscomp.getGlobal(this);
$jscomp.polyfill = function (a, c, b, d) {
  if (c) {
    b = $jscomp.global;
    a = a.split(".");
    for (d = 0; d < a.length - 1; d++) {
      var f = a[d];
      f in b || (b[f] = {});
      b = b[f];
    }
    a = a[a.length - 1];
    d = b[a];
    c = c(d);
    c != d &&
      null != c &&
      $jscomp.defineProperty(b, a, {
        configurable: !0,
        writable: !0,
        value: c,
      });
  }
};
$jscomp.polyfill(
  "Array.prototype.fill",
  function (a) {
    return a
      ? a
      : function (a, b, d) {
          var c = this.length || 0;
          0 > b && (b = Math.max(0, c + b));
          if (null == d || d > c) d = c;
          d = Number(d);
          0 > d && (d = Math.max(0, c + d));
          for (b = Number(b || 0); b < d; b++) this[b] = a;
          return this;
        };
  },
  "es6-impl",
  "es3"
);

var dispAry = [],
  clickRow = 0;

function onloadCpuDiag() {
  clickRow = 0;
  saveCurrentPage("inmenu4", "CpuDiag");
  mainTimerCycle = getStorage("mainTimerCycle", mainTimerCycle);
  monCpuStartStop(1);
  logoutBtnDisable(!1);
  for (var a = 0; 16 > a; a++)
    (dispAry[a] = []), (dispAry[a][0] = ""), (dispAry[a][1] = "");
}

function onCpuDiagTimerStart() {
  updateCpuDiagTable();
  timerID_cpudiag = setInterval(updateCpuDiagTable, 1e3 * mainTimerCycle);
}

function onCpuDiagTimerStop() {
  timerID_cpudiag && clearInterval(timerID_cpudiag);
}

function updateCpuDiagTable() {
  GetCpuDiagInf();
}

function monCpuStartStop(a) {
  onCpuDiagTimerStop();
  1 == a && onCpuDiagTimerStart();
}

function drawCanvas(a, c) {
  var b = getElIdObj("canvasarea" + (a + 1), 1),
    d = b.getContext("2d");
  d.clearRect(0, 0, b.width, b.height);
  d.beginPath();
  d.moveTo(5, 20);
  d.lineTo(15, 5);
  d.lineTo(25, 20);
  d.closePath();
  1 == c
    ? (d.fillStyle = "rgb(255,255,0)")
    : 2 == c
    ? (d.fillStyle = "rgb(255,127,0)")
    : 3 == c && (d.fillStyle = "rgb(255,0,0)");
  d.lineWidth = 3;
  d.stroke();
  d.fill();
}

function clearCanvas(a) {
  a = getElIdObj("canvasarea" + (a + 1), 1);
  a.getContext("2d").clearRect(0, 0, a.width, a.height);
}

function GetCpuDiagInf() {
  try {
    onTimerStopForW10();
    var a;
    a = new XMLHttpRequest();
    a.open("POST", "/cgi/GetErrInf.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      GetCpuDiagInf_Response(a);
    };
    a.send(null);
  } catch (c) {
    console.log(c.message);
  }
}

function GetCpuDiagInf_Response(a) {
  try {
    if (4 != a.readyState || 200 != a.status)
      3 == a.readyState && onTimerStartForW10();
    else {
      var c = JSON.parse(a.response),
        b = parseInt(c.NUM, 16);
      if ("0001" === c.RET) monCpuStartStop(0);
      else if ("0002" !== c.RET) {
        a = [];
        for (var d = 0; d < b; d++) {
          a[d] = [];
          var f,
            k,
            g = c.ERR[d][2].split("-"),
            h = c.ERR[d][3].split(":");
          f =
            ("0000" + parseInt(g[0], 16)).slice(-4) +
            "-" +
            ("00" + parseInt(g[1], 16)).slice(-2) +
            "-" +
            ("00" + parseInt(g[2], 16)).slice(-2);
          k =
            ("00" + parseInt(h[0], 16)).slice(-2) +
            ":" +
            ("00" + parseInt(h[1], 16)).slice(-2) +
            ":" +
            ("00" + parseInt(h[2], 16)).slice(-2);
          a[d][0] = f + "\r\n" + k;
          a[d][1] = c.ERR[d][1];
          a[d][2] = c.ERR[d][0];
          var l = changeFileName(c.ERR[d][5]);
          a[d][3] = l.replace(/^\s+|\s+$/g, "");
          a[d][4] = c.ERR[d][6].replace(/^\s+|\s+$/g, "");
          a[d][5] = c.ERR[d][4].replace(/^\s+|\s+$/g, "");
        }
        a.reverse();
        for (b = 0; b < a.length; b++) {
          getElIdObj("number" + (b + 1), 1).innerText = a.length - b;
          getElIdObj("daytime" + (b + 1), 1).innerText = a[b][0].split(" ");
          getElIdObj("state" + (b + 1), 1).innerText = getStateStr(
            parseInt(a[b][1])
          );
          drawCanvas(b, a[b][1]);
          getElIdObj("errorcode" + (b + 1), 1).innerText = a[b][2];
          var e = getErrorString(a[b][2]);
          getElIdObj("outline" + (b + 1), 1).innerText = e;
          dispAry[b][0] = getStepFileString(a[b][3], a[b][4]);
          dispAry[b][1] = getUnitNoString(a[b][5]);
        }
        for (e = b; 16 > e; e++)
          (getElIdObj("number" + (e + 1), 1).innerText = ""),
            (getElIdObj("daytime" + (e + 1), 1).innerText = ""),
            (getElIdObj("state" + (e + 1), 1).innerText = ""),
            clearCanvas(e),
            (getElIdObj("errorcode" + (e + 1), 1).innerText = ""),
            (getElIdObj("outline" + (e + 1), 1).innerText = "");
        "0000" == c.RET &&
          "00" == c.NUM &&
          0 == a.length &&
          ((getElIdObj("number1", 1).innerText = "1"),
          (getElIdObj("daytime1", 1).innerText = getErrorStr("msg_noerr")),
          (getElIdObj("errorcode1", 1).innerText = "-"),
          (getElIdObj("outline1", 1).innerText = "-"),
          (b = 1));
        if (0 != clickRow)
          if (b < clickRow)
            setLineColor(clickRow, "", ""),
              (clickRow = 0),
              (getElIdObj("file_strp", 1).innerText = "-"),
              (getElIdObj("slot", 1).innerText = "-");
          else {
            var m = getElIdObj("errTr" + clickRow, 1);
            onclickCpuRow(m, clickRow);
          }
      }
    }
  } catch (n) {
    console.log(n.message);
  }
}

function getErrorString(a) {
  for (var c = "", b = 0; b < ary_errcode.length; b++)
    if (a == ary_errcode[b][0]) {
      c = ary_gaiyou[ary_errcode[b][1]];
      break;
    }
  return c;
}

function getStateStr(a) {
  return stateString[a];
}

function getStepFileString(a, c) {
  var b = parseInt(c, 16);
  isNaN(b) && (b = "-");
  return stepNoString[0] + b + "\r\n" + stepNoString[1] + a;
}

function getUnitNoString(a) {
  var c = "-";
  if ("-" != a.charAt(0))
    for (var b = 0; b < slotNo_Tbl.length; b++)
      if (a == slotNo_Tbl[b][0]) {
        c = slotNo_Tbl[b][1];
        break;
      }
  return c;
}
function changeFileName(a) {
  var c = "";
  if (
    0 == a.length ||
    "00000000000000000000000000000000" == a ||
    "-                               " == a
  )
    c = "-";
  else
    for (var b = 0; b < a.length && "0000" != a.slice(b, b + 4); b += 4)
      c += String.fromCharCode(parseInt(a.slice(b, b + 4), 16));
  return c;
}

function onclickCpuRow(a, c) {
  if ("" != a.innerText) {
    clickRow = c;
    for (var b = 1; 16 >= b; b++) setLineColor(b, "", "");
    setLineColor(c, menuSelBgColor, "white");
    getElIdObj("file_strp", 1).innerText = dispAry[c - 1][0];
    getElIdObj("slot", 1).innerText = dispAry[c - 1][1];
  }
}

function onMouseOver(a) {
  var c;
  a != clickRow &&
    ((c = 0 == a % 2 ? LINE_HOVER_COLOR2 : LINE_HOVER_COLOR),
    setLineColor(a, c, LINE_HOVER_STRCOLOR));
}

function onMouseOut(a) {
  a != clickRow && setLineColor(a, "", "");
}

function setLineColor(a, c, b) {
  setBkCol("number" + a, c, 1);
  setBkCol("daytime" + a, c, 1);
  setBkCol("stateimage" + a, c, 1);
  setBkCol("canvasarea" + a, c, 1);
  setBkCol("state" + a, c, 1);
  setBkCol("errorcode" + a, c, 1);
  setBkCol("outline" + a, c, 1);
  setStlCol("number" + a, b, 1);
  setStlCol("daytime" + a, b, 1);
  setStlCol("stateimage" + a, b, 1);
  setStlCol("canvasarea" + a, b, 1);
  setStlCol("state" + a, b, 1);
  setStlCol("errorcode" + a, b, 1);
  setStlCol("outline" + a, b, 1);
}

function onunloadCpuDiag() {
  monCpuStartStop(0);
  logoutBtnDisable(!0);
}

//用户登录相关cgi
var LOGIN_LOCATION = "/cgi/login.cgi",
  LOGOUT_LOCATION = "/cgi/logout.cgi",
  GET_RANDOM_NUM = "/cgi/GetRndNum.cgi",
  USER_CHECK = /^[a-zA-Z0-9 !-/:-@\u00a5[-`{-~]+$/,
  PASS_LENGTH = /.{6,}/,
  PASS_CHECK = /^^[a-zA-Z0-9 !-/:-@\u00a5[-`{-~]+$/,
  userItem,
  passItem,
  msgItem,
  xhr,
  loginflg = 0,
  xhrTimeout_msec = 1e4;
function open_in_child() {
  top.location.href != self.location.href &&
    (top.location.href = "./Log-in.html");
}

function onloadLogin() {
  open_in_child();
  userItem = getElIdObj("user1");
  passItem = getElIdObj("pass1");
  msgItem = getElIdObj("msgArea");
}

function checkUser() {
  "" != userItem.value
    ? userItem.value.search(USER_CHECK)
      ? (userItem.className = "error")
      : ((userItem.className = "normal"), (msgItem.innerText = ""))
    : ((userItem.className = "normal"), (msgItem.innerText = ""));
}

function checkPass(a) {
  13 != a &&
    ("" != passItem.value
      ? passItem.value.search(PASS_LENGTH)
        ? (passItem.className = "error")
        : passItem.value.search(PASS_CHECK)
        ? (passItem.className = "error")
        : ((passItem.className = "normal"), (msgItem.innerText = ""))
      : ((passItem.className = "normal"), (msgItem.innerText = "")));
}

function LogInStart() {
  userItem.value.search(USER_CHECK)
    ? (userItem.className = "error")
    : passItem.value.search(PASS_LENGTH)
    ? (passItem.className = "error")
    : passItem.value.search(PASS_CHECK)
    ? (passItem.className = "error")
    : GetRndNum();
}


//登录
function LogIn(a, b) {
  try {
    var d = new MyHmacSha256().hmac_sha256(passItem.value, a),
      c;
    c =
      "USER=" +
      encodeURIComponent(userItem.value) +
      "&HASH=" +
      encodeURIComponent(d) +
      "&NO=" +
      b;
    xhr = new XMLHttpRequest();
    xhr.open("POST", LOGIN_LOCATION, !0);
    xhr.setRequestHeader(HDTYP, HDVAL);
    xhr.timeout = xhrTimeout_msec;
    xhr.ontimeout = function (a) {
      console.error("xhr.ontimeout");
      loginflg = 0;
      msgItem.innerText = alertMessage("devicetest_error", 1);
    };
    xhr.onerror = function (a) {
      console.error("xhr.onerror : [ " + a.type + " ] ");
      loginflg = 0;
      setTimeout(function () {
        loginflg = 0;
        msgItem.innerText = alertMessage("devicetest_error", 1);
      }, xhrTimeout_msec);
    };
    xhr.onreadystatechange = Login_Response;
    xhr.send(c);
  } catch (e) {
    (loginflg = 0), console.log(e.message);
  }
}
function Login_Response() {
  try {
    if (4 == xhr.readyState && 200 == xhr.status) {
      var a = JSON.parse(xhr.response);
      "0000" === a.RET
        ? (location.href = a.LOC)
        : "0002" === a.RET
        ? (location.href = a.LOC)
        : ((msgItem.innerText = alertMessage("login" + a.RET, 1)),
          (loginflg = 0));
    }
  } catch (b) {
    console.log(b.message), (loginflg = 0);
  }
}


function GetRndNum() {
  if (1 == loginflg) msgItem.innerText = alertMessage("devicetest_now", 1);
  else {
    loginflg = 1;
    try {
      (xhr = new XMLHttpRequest()),
        xhr.open("POST", GET_RANDOM_NUM, !0),
        xhr.setRequestHeader(HDTYP, HDVAL),
        (xhr.timeout = xhrTimeout_msec),
        (xhr.ontimeout = function (a) {
          console.error("xhr.ontimeout");
          loginflg = 0;
          msgItem.innerText = alertMessage("devicetest_error", 1);
        }),
        (xhr.onerror = function (a) {
          console.error("xhr.onerror : [ " + a.type + " ] ");
          loginflg = 0;
          setTimeout(function () {
            loginflg = 0;
            msgItem.innerText = alertMessage("devicetest_error", 1);
          }, xhrTimeout_msec);
        }),
        (xhr.onreadystatechange = GetRndNum_Response),
        xhr.send("NUM=32");
    } catch (a) {
      (loginflg = 0), console.log(a.message);
    }
  }
}

function GetRndNum_Response() {
  try {
    if (4 == xhr.readyState && 200 == xhr.status) {
      var a = JSON.parse(xhr.response);
      "0000" !== a.RET
        ? ((msgItem.innerText = alertMessage("login" + a.RET, 1)),
          (loginflg = 0))
        : LogIn(a.DATA, a.NO);
    }
  } catch (b) {
    (loginflg = 0), console.log(b.message);
  }
}

//登出
function LogOut() {
  try {
    (xhr = new XMLHttpRequest()),
      xhr.open("POST", LOGOUT_LOCATION, !0),
      xhr.setRequestHeader(HDTYP, HDVAL),
      (xhr.onreadystatechange = LogOut_Response),
      xhr.send(null);
  } catch (a) {
    console.log(a.message);
  }
}

function LogOut_Response() {
  try {
    4 == xhr.readyState &&
      200 == xhr.status &&
      ((logoutMsg = 1),
      JSON.parse(xhr.response),
      (location.href = "http://" + location.host));
  } catch (a) {
    console.log(a.message);
  }
}
function onKeydDwnPass(a) {
  13 == a && LogInStart();
}


var unitinfXhr,
  first = 1,
  unitRetry = 0,
  clockRetry = 0;

function onloadDefault() {
  first = 1;
  clockRetry = unitRetry = 0;
  saveCurrentPage("inmenu1", "Default");
  mainTimerCycle = getStorage("mainTimerCycle", mainTimerCycle);
  monStartStop(1);
  logoutBtnDisable(!1);
}

function onUnitInfTimerStart() {
  onUnitInfTimerStop();
  GetUnitInf();
  GetClockData();
}

function onUnitInfTimerStop() {
  timerID_default && clearInterval(timerID_default);
}

function updateUnitInfTable() {
  GetClockData();
}

//切换监控开始/停止
function monStartStop(a) {
  1 == a ? onUnitInfTimerStart() : onUnitInfTimerStop();
}

//获取模块信息
function GetUnitInf() {
  try {
    onTimerStopForW10(),
      (unitinfXhr = new XMLHttpRequest()),
      unitinfXhr.open("POST", "/cgi/GetUnitInf.cgi", !0),
      unitinfXhr.setRequestHeader(HDTYP, HDVAL),
      (unitinfXhr.onreadystatechange = GetUnitInf_Response),
      (unitinfXhr.onerror = function () {
        2 > unitRetry && (unitRetry++, GetUnitInf());
      }),
      unitinfXhr.send(null);
  } catch (a) {
    console.log(a.message);
  }
}
function GetUnitInf_Response() {
  try {
    if (4 !== unitinfXhr.readyState || 200 !== unitinfXhr.status)
      3 == unitinfXhr.readyState && onTimerStartForW10();
    else {
      var a = JSON.parse(unitinfXhr.response);
      if ("0000" === a.RET) {
        setUnitImg(a.NAME);
        setInTxt("model", a.NAME, 1);
        setInTxt("serialno", a.SERIALNO, 1);
        setInTxt("fwver", a.FWVER, 1);
        setInTxt("bootver", a.BOOTVER, 1);
        setInTxt("hwver", a.HWVER, 1);
        var b = [
          a.IPADDR.substring(0, 2),
          a.IPADDR.substring(2, 4),
          a.IPADDR.substring(4, 6),
          a.IPADDR.substring(6, 8),
        ];
        setInTxt(
          "ipaddr",
          parseInt(b[0], 16).toString() +
            "." +
            parseInt(b[1], 16).toString() +
            "." +
            parseInt(b[2], 16).toString() +
            "." +
            parseInt(b[3], 16).toString(),
          1
        );
        var c = [
          a.MACADDR.substring(0, 2),
          a.MACADDR.substring(2, 4),
          a.MACADDR.substring(4, 6),
          a.MACADDR.substring(6, 8),
          a.MACADDR.substring(8, 10),
          a.MACADDR.substring(10, 12),
        ];
        setInTxt(
          "macaddr",
          c[0] + "-" + c[1] + "-" + c[2] + "-" + c[3] + "-" + c[4] + "-" + c[5],
          1
        );
        setUnitName(a.LABEL, a.NAME);
      } else alertMessage(a.RET, 0);
    }
  } catch (d) {
    console.log(d.message);
  }
}

//获取时钟信息
function GetClockData() {
  try {
    onTimerStopForW10();
    var a;
    a = new XMLHttpRequest();
    a.open("POST", "/cgi/GetClockData.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      GetClockData_Response(a);
    };
    a.onerror = function () {
      2 > clockRetry && 1 === first && (clockRetry++, GetClockData());
    };
    a.send(null);
  } catch (b) {
    console.log(b.message);
  }
}

function GetClockData_Response(a) {
  try {
    if (4 != a.readyState || 200 != a.status)
      3 == a.readyState && onTimerStartForW10();
    else {
      var b = JSON.parse(a.response);
      1 === first &&
        ((timerID_default =
          5 > mainTimerCycle
            ? setInterval(updateUnitInfTable, 5e3)
            : setInterval(updateUnitInfTable, 1e3 * mainTimerCycle)),
        (first = 0));
      "0000" === b.RET
        ? (setInTxt(
            "day",
            parseInt(b.YEAR, 16) +
              "-" +
              ("0" + parseInt(b.MONTH, 16)).slice(-2) +
              "-" +
              ("0" + parseInt(b.DAY, 16)).slice(-2),
            1
          ),
          setInTxt(
            "time",
            ("0" + parseInt(b.HOUR, 16)).slice(-2) +
              ":" +
              ("0" + parseInt(b.MIN, 16)).slice(-2) +
              ":" +
              ("0" + parseInt(b.SEC, 16)).slice(-2),
            1
          ))
        : monStartStop(0);
    }
  } catch (c) {
    console.log(c.message);
  }
}

function onunloadDefault() {
  monStartStop(0);
  logoutBtnDisable(!0);
}

//菜单项相关
var menuBgColor = "rgb(221,221,221)",
  menuTextColor = "black",
  menuFontWgt = "normal",
  menuSelBgColor = "rgb(52,146,232)",
  menuSelTextColor = "white",
  menuSelFontWgt = "bold",
  currentMenuId,
  defCycle = 5,
  MAX_DISP_LINE = 32,
  reqRetry = 0,
  startPoint = 0,
  onloadFirst = !0;
function onloadMain() {
  "undefined" === typeof sessionStorage &&
    console.log("unsupported sessionStorage");
  mainTimerCycle = getStorage("mainTimerCycle", 5);
  mainSetType = getStorage("mainSetType", 0);
  mainDevType = getStorage("mainDevType", "D");
  mainDevAddress = getStorage("mainDevAddress", "");
  mainBfmUnitNo = getStorage("mainBfmUnitNo", "");
  mainBfmAddress = getStorage("mainBfmAddress", "");
  mainDataType = getStorage("mainDataType", 2);
  mainInputType = getStorage("mainInputType", "10");
  getStorageArray("mainDevRegData");
  mainBitsortSel = getStorage("mainBitsortSel", 0);
  viewPageNum = parseInt(getStorage("viewPageNum", 1));
  isBatMonitor = getStorage("isBatMonitor", 0);
  isRegMonitor = getStorage("isRegMonitor", 0);
  mainTimerCount = 0;
  setElIdVal("reloadcycle", mainTimerCycle);
  var a = JSON.stringify(ary_errcode);
  setStorage("mainErrCode", a);
  a = JSON.stringify(ary_gaiyou);
  setStorage("mainErrOutline", a);
  var a = getElIdObj("selectlanguage").lang,
    b = "0";
  "ja" == a ? (b = "0") : "en-us" == a ? (b = "1") : "zh-cn" == a && (b = "2");
  setElIdVal("selectlanguage", b);
  modalCloseDevTest();
  onloadMenu();
}

function onloadMenu() {
  focusTab();
  var a = getStorage("mainCpuImg", "");
  "" == a
    ? GetUnitName()
    : ((getElIdObj("cpuimage").src = a),
      setUnitImg(a),
      (document.title = getStorage("mainUnitLabel", "")));
  onTimerStart();
}

function onclickMenu(a) {
  try {
    (currentMenuId = a), focusTab();
  } catch (b) {
    console.log(b.message);
  }
}

function onTimerStart() {
  onTimerStop();
  mainTimerCycle = getElIdVal("reloadcycle");
  setStorage("mainTimerCycle", mainTimerCycle);
  -1 != window.navigator.userAgent.indexOf("Windows NT 10.0") &&
    GetHTTPCloseTimer();
  updateInfo();
  mainTimerID_menu = setInterval(updateInfo, 1e3 * mainTimerCycle);
}
function onTimerStop() {
  mainTimerID_menu && (clearInterval(mainTimerID_menu), (mainTimerCount = 0));
}
function updateInfo() {
  mainTimerCount += 1;
  mainTimeOutID || (mainTimeOutID = setTimeout(returnLoginPage, 3e5));
  GetLedStatus();
}


function GetHTTPCloseTimer() {
  try {
    var a;
    a = new XMLHttpRequest();
    a.open("POST", "/cgi/RdDevBlk.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      GetHTTPCloseTimer_Response(a);
    };
    a.send("NUM=1&DEV=SD11075");
  } catch (b) {
    console.log(b.message);
  }
}

function GetHTTPCloseTimer_Response(a) {
  try {
    if (4 == a.readyState && 200 == a.status) {
      var b = JSON.parse(a.response);
      timeout_10 = "0000" === b.RET ? 100 * parseInt(b.DATA[0], 16) : 9999999;
    }
  } catch (c) {
    console.log(c.message);
  }
}

function onbuttonReloadCycle(a) {
  13 === a && (checkCycle(), onTimerStart(), viewarea.location.reload(!0));
}

function checkCycle() {
  var a = getElIdObj("reloadcycle");
  if ("" === a.value) a.value = defCycle;
  else if (a.value.search(/^[0-9]+$/)) a.value = defCycle;
  else {
    var b = Number(a.value);
    if (5 > b || 120 < b) a.value = defCycle;
  }
}


function focusTab() {
  menuBgColor = getElIdObj("menu").style.backgroundColor;
  menuTextColor = getElIdObj("menu").style.color;
  menuFontWgt = getElIdObj("menu").style.fontWeight;
  for (var a = 1; 6 >= a; a++)
    setBkCol("inmenu" + a, menuBgColor),
      setStlCol("inmenu" + a, menuTextColor),
      (getElIdObj("inmenu" + a).style.fontWeight = menuFontWgt);
  currentMenuId &&
    (setBkCol(currentMenuId, menuSelBgColor),
    setStlCol(currentMenuId, menuSelTextColor),
    (getElIdObj(currentMenuId).style.fontWeight = menuSelFontWgt));
}

//获取PLC模块信息
function GetUnitName() {
  try {
    var a = new XMLHttpRequest();
    a.open("POST", "/cgi/GetUnitInf.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      GetUnitName_Response(a);
    };
    a.onerror = function () {
      2 > reqRetry && (reqRetry++, GetUnitName());
    };
    a.send(null);
  } catch (b) {
    console.log(b.message);
  }
}

//解析模块信息内容
function GetUnitName_Response(a) {
  try {
    if (4 === a.readyState && 200 === a.status) {
      var b = JSON.parse(a.response);
      "0000" === b.RET
        ? (setUnitImg(b.NAME), setUnitName(b.LABEL, b.NAME))
        : alertMessage(b.RET, 0);
    }
  } catch (c) {
    console.log(c.message);
  }
}

//设置切换显示语言
function changeLanguage() {
  var a;
  "0" === getElIdVal("selectlanguage")
    ? (a = "ja-JP")
    : "1" === getElIdVal("selectlanguage")
    ? (a = "en-US")
    : "2" === getElIdVal("selectlanguage") && (a = "zh-CN");
  top.location.href = "./" + ("Main_" + a + ".html");
}

function setUnitName(a, b) {
  var c = "";
  if (
    "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" ==
      a ||
    "undefined" == a
  )
    c = b;
  else
    for (var d = 0; d < a.length && "0000" != a.slice(d, d + 4); d += 4)
      c += String.fromCharCode(parseInt(a.slice(d, d + 4), 16));
  document.title = c;
  setStorage("mainUnitLabel", c);
}

//设置显示FX5U还是FX5UC
function setUnitImg(a) {
  -1 !== a.indexOf("FX5UC")
    ? (setStorage("mainCpuImg", "./FX5UC-32CPU.png"),
      (parent.document.getElementById("cpuimage").className = "fx5ucimg"),
      (parent.document.getElementById("cpuimage").src = "./FX5UC-32CPU.png"))
    : (setStorage("mainCpuImg", "./FX5U-32CPU.png"),
      (parent.document.getElementById("cpuimage").className = "fx5uimg"),
      (parent.document.getElementById("cpuimage").src = "./FX5U-32CPU.png"));
  parent.document.getElementById("cpuimage").onerror = function () {
    onNotice();
    alertErr();
  };
}

//LED相关
var colGreen = "rgb(124,252,0)",
  colRed = "rgb(255, 0, 0)",
  colBlack = "rgb(0, 0, 0)";


function GetLedStatus() {
  try {
    onTimerStopForW10();
    var a;
    a = new XMLHttpRequest();
    a.open("POST", "/cgi/GetLedInf.cgi", !0);
    a.setRequestHeader(HDTYP, HDVAL);
    a.onreadystatechange = function () {
      GetLedStatus_Response(a);
    };
    a.send(null);
  } catch (b) {
    console.log(b.message);
  }
}

//设置LED状态闪灯显示
function setLedStatus(a, b, c) {
  "1" === a
    ? ((b.style.backgroundColor = colBlack), (b.style.animation = ""))
    : "4" === a
    ? ((b.style.backgroundColor = c), (b.style.animation = ""))
    : ((b.style.backgroundColor = c),
      (b.style.animation =
        colGreen === c
          ? "blinkgreen 0.2s linear infinite alternate"
          : "blinkred 0.2s linear infinite alternate"));
}

//获取LED状态显示
function GetLedStatus_Response(a) {
  try {
    if (4 !== a.readyState || 200 !== a.status)
      3 == a.readyState && onTimerStartForW10();
    else {
      mainTimeOutID && (clearTimeout(mainTimeOutID), (mainTimeOutID = 0));
      var b = JSON.parse(a.response);
      "0001" === b.RET
        ? (onTimerStop(), a && a.abort(), alertMessage(b.RET, 0))
        : ((document.getElementById("pwrled").style.backgroundColor = colGreen),
          setLedStatus(b.LED[0], document.getElementById("errled"), colRed),
          setLedStatus(b.LED[1], document.getElementById("runled"), colGreen),
          setLedStatus(b.LED[2], document.getElementById("batled"), colRed));
    }
  } catch (c) {
    console.log(c.message);
  }
}

//
